Distant_storm Posted November 16, 2007 Share Posted November 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
revraz Posted November 16, 2007 Share Posted November 16, 2007 Sessions are supposed to reset when you close your browser. Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 17, 2007 Author Share Posted November 17, 2007 How can I get it to stop ending the session when the browser closes. If this code is somewhere and presented simply in another post feel free to redirect me to it. Thanks. Quote Link to comment Share on other sites More sharing options...
alecks Posted November 17, 2007 Share Posted November 17, 2007 I don't think that is possible; as said above sessions are only open as long as the user's browser is. 'Remember me' features generally use cookies to start a user's session again. Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 Yep, that's the difference between cookies and session cookies! Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 17, 2007 Author Share Posted November 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 They don't, that's why their not session cookies... Quote Link to comment Share on other sites More sharing options...
toplay Posted November 17, 2007 Share Posted November 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 17, 2007 Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
toplay Posted November 17, 2007 Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 17, 2007 Author Share Posted November 17, 2007 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 ! Quote Link to comment 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.