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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.