Jump to content

[SOLVED] Session ending when browser closed


Distant_storm

Recommended Posts

I have a problem with my sessions, it is for storing very simple data.

 

a) name

b) ip

c) time

 

 

my set up of the session ini goes as so...

 

 

ini_set('session.gc_maxlifetime', time() + 300);

ini_set('session.cookie_lifetime', time() + 300);

session_start();

 

$_SESSION['name'] = "name";

$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];

$_SESSION['time'] = time();

 

Now when checking these i use this

 

if (!isset($_SESSION['name'])) {

 

set session balh blah with the above ini settings

 

} else {

 

retrive the values

 

}

 

 

 

ok so is their any reason why that should everytime i close my browser start a new session?

 

Its nothing to do with my cookie settings on my computer because all other sites are fine.

 

Much apreciated for your help

 

Matt

 

Link to comment
Share on other sites

PHP sessions can last after the browser is closed but it's not recommended.

 

You are setting the right variable names but you're setting them wrong with using time(). Also, 300 is just 5 minutes.

 

Set session.cookie_lifetime to the number of seconds you want the session cookie to last (i.e. 2592000 seconds = 30 days). The default is 0 which means the session cookie will last until the browser is closed. Set the number of cookie seconds a little longer than the session.gc_maxlifetime. See manual:

http://us2.php.net/manual/en/ref.session.php#ini.session.cookie-lifetime

http://us2.php.net/manual/en/ref.session.php#ini.session.gc-maxlifetime

 

Make sure session.use_cookies is enabled (which it is by default):

http://us2.php.net/manual/en/ref.session.php#ini.session.use-cookies

 

Link to comment
Share on other sites

If you have tried to use both sessions and cookies and they both don't work, it is highly likely that your code is outputting content to the browser prior to the session_start() or the setcookie(). Either check your web server log for errors and/or turn on full php error reporting. Also, posting your actual code would help in allowing someone to see what you are actually doing in it.

Link to comment
Share on other sites

Ahh well in that case Id prefer to use cookies. Does anyone know how to stop cookies from expiring after the browser is closed ?

so forgetting sessions all together and just cookies instead. Sorry if this topic is already covered elswhere

 

There's example code on how to set cookie expiration on this manual page:

http://us2.php.net/manual/en/function.setcookie.php

 

setcookie() is where you can use the time()+3600 type of notation.

 

 

Link to comment
Share on other sites

Thanks for all your help people massive amount of responses. I have used some cookie settings. The data isnt that at risk so its not so important to use sessions although I am always looking to improve on php so sessions cookies is something i must read up on. I think in the code i posted up above I forgot to increase the cookie max lifetime more than the gc maxlifetime.

 

Thanks alot for people who put in alot of detail really cleared things up for me

 

Great support from you lot ! :)

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.