how to redirect the session value to next page in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

how to redirect the session value to next page in php

Post by manojsemwal1 »

hai friend iam new in php.
iam making a php application and checking username and password against DB.i have a problem how can i redirect the session value(username) to aunthonticate page.
my coding are like

Code: Select all

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
 
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
echo $myusername;
$_SESSION['myusername'] = $myusername;
echo $_SESSION['myusername'];
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
in login_success.php the username value.is not showing
Last edited by Benjamin on Tue Jun 30, 2009 12:11 am, edited 1 time in total.
Reason: Added [code=php] tags.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: how to redirect the session value to next page in php

Post by Eric! »

Change your first script to use session_start

Code: Select all

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
 
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
//session_register("myusername");
//session_register("mypassword"); never put passwords in session data
 
echo $myusername;
$_SESSION['myusername'] = $myusername;
echo $_SESSION['myusername'];
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush(); //this does nothing
?>

login_success.php needs the following

Code: Select all

session_start();
if(!isset($_SESSION['myusername']))
   // redirect to previous page
  else // user is logged in
 
It is also good practice to hash your passwords and check your input fields for sql injection attacks.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to redirect the session value to next page in php

Post by manojsemwal1 »

Thanks Eric for your support.

Ya its showing the session value. but i have many user and and each user having their login id and password .when the new user login the last user session value is changed with new session value.
how to solve it pl help me.

waiting ur reply...
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: how to redirect the session value to next page in php

Post by Eric! »

manojsemwal1 wrote:when the new user login the last user session value is changed with new session value.
I don't understand what the problem is. Can you explain this in more detail?
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: how to redirect the session value to next page in php

Post by a94060 »

the sessions should be seperate from each other. If user A logs on from one place,their session will be seperate from User B from another place. When the users log out,their sessions are destroyed. From what i understand, it seems that the statement has something to do with session contamination or something,which(to the best of my knowledge) cannot occur in php
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to redirect the session value to next page in php

Post by manojsemwal1 »

i agree with a94060 but in my form when user A is login then in welcome note User Name A is displaying but when user B login then in last session value User Name A is destroyed and User B name is showing in User A login Form Also.

regards,

Manoj
Post Reply