Page 1 of 1

how to redirect the session value to next page in php

Posted: Mon Jun 29, 2009 6:41 am
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

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

Posted: Mon Jun 29, 2009 8:20 am
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.

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

Posted: Mon Jun 29, 2009 8:37 am
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...

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

Posted: Mon Jun 29, 2009 11:22 am
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?

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

Posted: Mon Jun 29, 2009 11:41 am
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

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

Posted: Mon Jun 29, 2009 11:09 pm
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