Forum Moderators: coopster

Message Too Old, No Replies

php advanced session database

         

JuicyScript

6:24 pm on Jul 29, 2010 (gmt 0)

10+ Year Member



i created sessions for my page...but i want to store session data so that when the user logs out from a page,and when he logs back he can start from where he stopped

This is what i have done so far

login_processor
<?php session_start();
// Developed by Roshan Bhattarai
// Visit [roshanbh.com.np...] for this script and more.
// This notice MUST stay intact for legal use

//Connect to database from here
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//select the database | Change the name of database from here
mysql_select_db('healthreg');

//get the posted values
$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES);
$pass=md5($_POST['password']);

//now validating the username and password
$sql="SELECT * FROM members WHERE user_name='".$user_name."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

//if username exists
if(mysql_num_rows($result)>0)
{
//compare the password
if(strcmp($row['password'],$pass)==0)
{
echo "yes";
$member = mysql_fetch_assoc($result);
//now set the session from here if needed
$_SESSION['u_name']=$user_name;
//$_COOKIE['u_name']=$user_name;
$_COOKIE['SESS_MEMBER_ID'] = $member['member_id'];
}
else
echo "no";
}
else
echo "no"; //Invalid Login


?>

<!--Session check on every page except login page-->
<?php
//Start session
session_start();
// setcookie($_COOKIE['SESS_MEMBER_ID']);

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['u_name']) || (trim($_SESSION['u_name']) == '')) {
echo "not picking session";

if (isset($_COOKIE['SESS_MEMBER_ID']) && isset($_COOKIE['SESS_MEMBER_ID'])) {
echo "Success";
} else {
echo "Failed";
}

exit();
}
?>

<!--logout-->
<?php
//Start session
session_start();

//Unset the variables stored in session
unset($_SESSION['u_name']);

?>

Matthew1980

7:54 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there JuicyScript,
$_COOKIE['SESS_MEMBER_ID'] = $member['member_id'];


If this is where you are setting the/assigning the $_COOKIE I don't think as it will register because you aren't using the setcookie(); function, check the registered cookie's in your browsers options to see if you are actually setting anything, which at the moment I doubt.

Check out the write-up on setcookie() to see how to set a cookie:[uk2.php.net ] because at the moment you are assigning the $member['member_id'] to something that isn't set (at least I don't think you can set cookie's like this).

I personally would turn on the error_reporting(E_ALL); at the top of the script, this will enable you to see any error's that are being flagged up that you are missing.

Also, your missing braces from the 'else' clauses:-

if(mysql_num_rows($result)>0)
{
//compare the password
if(strcmp($row['password'],$pass)==0)
{
echo "yes";
$member = mysql_fetch_assoc($result);
//now set the session from here if needed
$_SESSION['u_name']=$user_name;
//$_COOKIE['u_name']=$user_name;
$_COOKIE['SESS_MEMBER_ID'] = $member['member_id'];
}
else{
echo "no";
}
else{
echo "no"; //Invalid Login
}


Hope this gives you an idea of what's going wrong.. Bearing in mind I have only skimmed the code - so I could have missed things, I have only noted what stands out ;)

Cheers,
MRb

rocknbil

5:41 pm on Jul 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your PHP sessions will die, but you're on the right track - use a cookie to pick up from where you left off.
It's important to note though, set a valid expire date on a cookie. If you set a cookie without a valid expiration date, it is a session cookie (browser session, not PHP session.) Close the browser, the cookie dies. You can set cookies for an expiration date far in the future.