atmega-ist Posted May 31, 2010 Share Posted May 31, 2010 Hello, all - the subject pretty much says it.. my session variables are fine while navigating the site. they even hold wen i navigate completely away from the server and then come back but as soon as i close the browser they all go blank... it was my understanding that they would remain after close. is this incorrect and i just need to make a full-on cookie? The pages below flow as: index.php -> login.php -> member.php Thanks! index.php: <html> <form action="login.php" method="POST"> Username: <input type="test" name="username"><br> Password: <input type="password" name="password"><br> <input type="submit" name="btnlogin" value="Log In"> </form> </html> login.php: <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; if ($username&&$password) { $connect = mysql_connect("localhost", "root", "map2131") or die("Couldn't connect"); mysql_select_db("login")or die("no such database found"); $query = mysql_query("SELECT * FROM logintable WHERE username='$username'"); $numrows = mysql_num_rows($query); if ($numrows!=0) { while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; } if ($username==$dbusername&&$password==$dbpassword) { echo "Logged in. Click <a href='member.php'>here</a> to enter the member page"; $_SESSION['splat']=$dbusername; } else echo "Incorrect Password"; echo $row; } else die("No such user."); } else die("Please enter username <u>and</u> password"); ?> member.php: <?php session_start(); echo "Welcome, ".$_SESSION['splat']."!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/203469-session-var-not-saving-after-browser-close/ Share on other sites More sharing options...
trq Posted May 31, 2010 Share Posted May 31, 2010 it was my understanding that they would remain after close. That is indeed incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/203469-session-var-not-saving-after-browser-close/#findComment-1065915 Share on other sites More sharing options...
premiso Posted May 31, 2010 Share Posted May 31, 2010 That is because the session cookies lifetime (<-- link) is being set to 0, which is default and means that the session expires when the browser close. To fix this you will have to change that value to be the lifetime you want the session cookie to last. I cannot remember what the variable names are, but should be easily found with google or even at the php manual for sessions Quote Link to comment https://forums.phpfreaks.com/topic/203469-session-var-not-saving-after-browser-close/#findComment-1065918 Share on other sites More sharing options...
atmega-ist Posted May 31, 2010 Author Share Posted May 31, 2010 premiso - a) apologies. I keep forgetting about the manual. b) thanks a TON for the quick instruction. updated the code as shown below (in case anyone else has the same question) and everything works perfectly! thanks again! session_cache_expire(20); session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/203469-session-var-not-saving-after-browser-close/#findComment-1065921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.