Jump to content

Session var not saving after browser close


atmega-ist

Recommended Posts

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']."!";
?>

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

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();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.