LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-15-2007, 07:31 PM   #1
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Rep: Reputation: 15
Redirection in PHP after IF / ELSEIF / ELSE statements


Scroll down to later in the topic.

Last edited by Jorophose; 06-20-2007 at 06:26 PM.
 
Old 06-16-2007, 12:18 PM   #2
FMC
Member
 
Registered: May 2007
Location: São Paulo
Distribution: Gentoo & Debian
Posts: 97

Rep: Reputation: 15
I will try to tell the basic things that you will need.

First of all: php.net is great!

To identify the user you can start a session with
Code:
http://php.net/session_start
session_start();
Than you can register an array to identify the user and keep some informations about this person.

Code:
http://php.net/session_register
session_register();
You can put this code on an include file and include it on every page. Sure, just register the session when the user type the correct username/password.

To check the user name you can read the database with mysql_* functions, create an include file with your connection informations to make it easy:

Code:
http://php.net/mysql_connect
mysql_connect("server", "username", "password");
Don't forget to select a database:
Code:
http://php.net/mysql_select_db
mysql_connect("your_database");
Now you already have a open link and you can use some functions to check things and etcetera:
Code:
http://php.net/mysql_fetch_array

if($user = mysql_fetch_array(mysql_query("SELECT id, group FROM user WHERE name = '$_POST[username]' AND password = '$_POST[password]' AND active = 1"))):
   session_register("$UserInfo");
   $_SESSION[UserInfo][id] = $user[id];
   $_SESSION[UserInfo][group] = $user[group];
else:
   echo "<form to input login information>";
endif;
You can also use mysql_result to get a specific result:
Code:
http://php.net/mysql_result
$full_name = mysql_result(mysql_query("SELECT full_name FROM user WHERE id = $UserInfo[id]"),0,0);
To insert values you can just run mysql_query:
Code:
http://php.net/mysql_query
$query = "INSERT INTO user (name, password, fullname) VALUES ('foo', 'bar', 'Foo Bar Baz');
if(!@mysql_query($query)):
    echo "Error inserting values";
endif;
If you need a lot of results you can run mysql_fetch_array in a fashion way:
Code:
http://php.net/mysql_fetch_array
$query = mysql_query("SELECT id, user, fullname FROM user WHERE active = 1");
while($res = mysql_fetch_array($query)):
   echo "$res[id] - $res[user] - $res[fullname]<br>";
endwhile;
Be careful with mysql_injections, to avoid this kind of problem you will have to read about mysql_escape_string and mysql_real_escape_string on php.net:
http://php.net/mysql_escape_string
http://php.net/mysql_real_escape_string

Well, I believe that you have some material to start building your app, go for it... and if you have some doubt just ask!

[]'s, FMC!
 
Old 06-16-2007, 03:41 PM   #3
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Original Poster
Rep: Reputation: 15
Thanks FMC for the tips. I had used the steps here (http://www.php-mysql-tutorial.com/us...n/database.php) but they don't seem to be working well.

Regarding the code you gave me, does it all go inside the same PHP file? (Except for the one that is used with php include().) Do I add the usernames & passwords myself? Do I need to include the php.net links, or are they just for refference?

And is there any way to keep track of stuff a user has picked up/achieved?

For inserting usernames & passwords, can I do that manually in a database? Or is there a SQL script/program/etc. I can use to do it for me?

Thanks!
 
Old 06-16-2007, 05:38 PM   #4
FMC
Member
 
Registered: May 2007
Location: São Paulo
Distribution: Gentoo & Debian
Posts: 97

Rep: Reputation: 15
Well, I just gave you some tips, you will have to use your creativity to put everything together and build your app!

The links to php.net are just for reference, there you can know everything the function does (or do, I don't know, I'm braziliam)!

The first important thing to start to code is to be sure what your software will be, you have to imagine how it will work and everything, try to imagine something like:
1 - A form to input log/pass.
2 - If the log/pass is correct, than register the session and redirect the user to the initial page, it it is incorrect show the form again.
3 - Show something to the user and let him decide what to do.
4 ...

I don't know exactly what you want to do, even if I knew it would be impossible to me to give you the exact lines that you need, that's why I'm linking everything to you, this way you can study and do it by yourself!

You could start by doing something like:
connection.inc.php (a include for every single page)
login.inc.php (to be sure that the user is logged in, if not, redirect to log page)
login.php (form for the user to login and register the session)
index.php (page to show something for the user after login)

Oh, another function that you will probably like is "header", with this function you can redirect the user to another page: php.net/header (search for "Location").

[]'s, FMC!
 
Old 06-16-2007, 05:46 PM   #5
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Original Poster
Rep: Reputation: 15
Ah, thanks for the clarification.

I should be good to go now, so I guess have to test it out and see what happens now.

Cheers,
Joro.
 
Old 06-17-2007, 12:27 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
Just in case you don't have it, here's the online refs to all the MySQL docs, inc examples, for all version from 3 -> 5; see top left section of page for the link to your version.
http://dev.mysql.com/doc/refman/5.0/en/
 
Old 06-17-2007, 06:15 PM   #7
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Original Poster
Rep: Reputation: 15
Thanks chrism, I'll have to check that link out later though, right now I've got an impeding problem.

http://slycorps.6te.net/quest/
If you go there, and type in the word bar, you get a page that tells you "you're in a bar" but if you type in phonebooth, it tells you "you're in a phonebooth" and then gives you the error message right after.

Code:
<html> 
<head> 
<title>POST and GET functions</title> 
</head> 
<body> 

<?php 
$destination = $_POST["destination"];   /* This will get the name variable input from the form above */ 
$booth = 'phonebooth';        /* This should make the term booth in the box mean booth here */ 
$bar = 'bar';            /* See above, but the bar */ 

if ($destination == $booth){ 
   echo "You are in a telephone booth."; 
} 

if ($destination == $bar){ 
   echo "You are in a bar."; 
} 

else { echo "Sorry, I don't understand what you meant by $destination :("; } 

?> 

</body> 
</html>
Here's my code, I'm hoping someone can help me spot the problem. This is possibly my first serious shot at programming something, and I had to work some issues out myself. Also, is there any way to redirect a user to another page rather than use "echo"?
 
Old 06-17-2007, 06:32 PM   #8
FMC
Member
 
Registered: May 2007
Location: São Paulo
Distribution: Gentoo & Debian
Posts: 97

Rep: Reputation: 15
As I already told, you can use the header function to redirect the user to another page:

header("Location /yourpage.php");

[]'s, FMC!
 
Old 06-17-2007, 06:45 PM   #9
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Original Poster
Rep: Reputation: 15
EDIT: This is getting crazy. Turns out closing the brackets after each echo, and making repetitive statements "elseif" works.

But how do I do header functions?

Last edited by Jorophose; 06-17-2007 at 08:31 PM.
 
Old 06-19-2007, 04:52 AM   #10
matthewhardwick
Member
 
Registered: Oct 2003
Location: CA
Posts: 165

Rep: Reputation: 30
I know I may be smacked in the head for saying this, but Dreamweaver can do all this and more. You can build a comprehensive system from the ground up very quickly and easily, and it will look after SQL and Users etc. for you.
 
Old 06-19-2007, 12:19 PM   #11
gamma9mu
LQ Newbie
 
Registered: Mar 2006
Location: LitH, IL
Distribution: Ubuntu Feisty
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by matthewhardwick
I know I may be smacked in the head for saying this, but Dreamweaver can do all this and more. You can build a comprehensive system from the ground up very quickly and easily, and it will look after SQL and Users etc. for you.
will dreamweaver teach you PHP and MySQL?
 
Old 06-19-2007, 05:04 PM   #12
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Original Poster
Rep: Reputation: 15
It probably won't. And on top of that, I don't have 400$ (Or whatever amount it is now) to spend on Dreamweaver, I don't think I can run it well, and school's out so I can't use the labs.

I still haven't figured out how to use "header" after an if/elseif/else statement. Any ideas? And can I send a user back to a page + echo something new?
 
Old 06-20-2007, 06:14 AM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
Also, afaik, Dreamweaver has never been ported to Linux ... unless you're using virtual OS.
Anyway, you won't learn as much that way.
 
Old 06-20-2007, 06:25 PM   #14
Jorophose
Member
 
Registered: Oct 2006
Location: Ontario, Canada
Distribution: Xubuntu 6.06!! =D
Posts: 137

Original Poster
Rep: Reputation: 15
Plus it doesn't seem to be working under Wine right now, and I doubt CrossOver has a solution...

Man, if only Adobe would bring the creative suite to Linux. I mean, they can keep it closed if they want... But native Photoshop on Linux is a huge boon.

[/offtopic]

Guys, I could use a bit of help here!

I can't make that bloody "header" function do anything when it's in an IF statement! Any fixes?
 
Old 06-20-2007, 06:50 PM   #15
FMC
Member
 
Registered: May 2007
Location: São Paulo
Distribution: Gentoo & Debian
Posts: 97

Rep: Reputation: 15
Do you receive any warn message when you execute your if statement?

The header function will not work if there is some output to browser before it is called. A simple \n is sufficient to make header not work at all!

Try this code and take it as a example:
Code:
<?php
if($_GET[test] == 1):
    header("Location: http://www.google.com");
elseif($_GET[test] == 2):
    header("Location: http://www.yahoo.com");
else:
    echo "<a href=\"$PHP_SELF?test=1\">click here to go to google</a><br>
          <a href=\"$PHP_SELF?test=2\">click here to go to yahoo </a><br>
         ";
endif;
?>
[]'s, FMC!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware guide for AMP Apache MySQL & PHP xushi Slackware 35 05-11-2006 11:18 AM
Apache2, MySQL & PHP Web Server. FTP Needed! Bobson Linux - Networking 4 02-07-2005 04:25 PM
PHP and MySQL for web design - installing and further r3dhatter Programming 1 06-24-2004 02:10 AM
PHP & MySQL Count on PHP Gerardoj Programming 1 06-01-2004 10:43 AM
From RedHat9 to FreeBSD (Apache 2 & Mysql & PHP 4) guardian653 *BSD 5 12-11-2003 05:31 PM


All times are GMT -5. The time now is 04:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration