jayjay960 Posted July 22, 2009 Share Posted July 22, 2009 Ok, this is driving me insane. Here is a script that runs at the start of each page (before any html): <?php $rememberme = false; if (isset($_POST['rememberme'])) { $rememberme = true; $logintime = time(); } if ($rememberme || isset($_SESSION['rememberme'])) { session_set_cookie_params($_SESSION['logintime']+(60*60*24*30)); } session_start(); if ($rememberme) { $_SESSION['rememberme'] = true; } ?> What I'm trying to do is have it so that when you login and have checked the checkbox named 'rememberme', the session cookie lasts for 30 days. Currently, if you log in without checking the box the cookie will expire at the end of the session as normal, however, if you tick the remember me box, the exact same thing happens. No expiry date. What's annoying me is that if I put session_set_cookie_params(60*60*24*30); directly before session_start();, it works fine and the cookie has an expiry date, but once I try to check if the user chose remember me, it stops working. The first thing I tried was simply: [code]<?php if (isset($_POST['rememberme'])) { session_set_cookie_params(60*60*24*30); } session_start(); ?> But that didn't work, and since then I've tried every possible way of doing this I could think of, including destroying the session cookie before setting the cookie params, and even just setcookie('PHPSESSID', session_id(), '', 60*60*24*30);, and I've had different results each time, including (from the look of it) my $_SESSION variables being reset, being able to log in without checking the box but not with, and getting errors from session_start() that say my headers have already been sent. I would really, really appreciate help with this, it's driving me insane.. Quote Link to comment https://forums.phpfreaks.com/topic/166928-sessions/ Share on other sites More sharing options...
irkevin Posted July 22, 2009 Share Posted July 22, 2009 Just a suggestion. Try to put this session_start(); On top of your codes. Just after the opening php tag like this: <?php session_start(); //the rest of your codes ?> Quote Link to comment https://forums.phpfreaks.com/topic/166928-sessions/#findComment-880134 Share on other sites More sharing options...
jayjay960 Posted July 22, 2009 Author Share Posted July 22, 2009 session_set_cookie_params() has to come before session_start() Quote Link to comment https://forums.phpfreaks.com/topic/166928-sessions/#findComment-880136 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2009 Share Posted July 22, 2009 A session is intended to last one browser session (that's where it gets its name from.) You can extend a session to last longer than one browser session but you must set both the session.cookie_lifetime and the session.gc_maxlifetime and if you are on a shared web server where the session.save_path is the default location, you must set to to a private folder within your account's folder tree so that you session setting only apply to your session data files. All of these settings must be set before every session_start() (globally putting them into the master php.ini, a local php.ini, or a .htaccess file is the recommend method.) Short answer - to implement a "remember me" feature, it is best to use a regular cookie and don't attempt to extend the session. Use a session for its' intended purpose, to hold variables that you want to persist during one browser session. Quote Link to comment https://forums.phpfreaks.com/topic/166928-sessions/#findComment-880391 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.