aeroswat Posted January 12, 2010 Share Posted January 12, 2010 Does the function session_start() being called refresh my session time? Also is there a way to set a longer expiration of the cookie other than changing the phpini? I'm currently on a shared host and I doubt they'd let me do that. I will be changing to my own server soon but this seems illogical to have to change the ini file to keep a user logged in longer than 10 minutes without activity... Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/ Share on other sites More sharing options...
Mchl Posted January 12, 2010 Share Posted January 12, 2010 Yes. It tells the server that session's in use. setcookie's third parameter is for setting expiration time. Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-993830 Share on other sites More sharing options...
aeroswat Posted January 12, 2010 Author Share Posted January 12, 2010 Yes. It tells the server that session's in use. setcookie's third parameter is for setting expiration time. In order to set an infinitely long session would i do this before my initial call of session_start() session_set_cookie_params(0, '/', 'sitename.com'); Also if you are familiar with javascript/jquery would you mind taking a look at this? My post is getting no love in those forums http://www.phpfreaks.com/forums/index.php/topic,283891.0.html Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-993833 Share on other sites More sharing options...
PFMaBiSmAd Posted January 12, 2010 Share Posted January 12, 2010 The session cookie lifetime determines how long the session id cookie will remain valid after all instances of the browser is closed. A zero value (the default setting) means "until the browser is closed". A non-zero value means that the session id cookie expiration time will be set to that number of seconds added to the current server's time. A session will exist as long as the browser supplies a valid session id and the corresponding session data file exists on the server. To extend a session, you would either need to periodically 'refresh' the last access time of the session data file (by executing a session_start() statement) to prevent session garbage collection from deleting the session data file or you would need to extend the session.gc_maxlifetime value. On a shared web server where you are using the default/common tmp session.save_path setting, the shortest session.gc_maxlifetime setting of all the scripts running on the server will win. So, you also need to set the session.save_path to be to a 'private' folder within your account's folder tree if you want you session settings to apply only to your session data files. All values affecting the session id cookie or the session data files must be set before every session_start() statement. It is usually best to globally set them in the master php.ini (when you have access to it), in a .htaccess file (when php is running as a Apache Module), or in a local php.ini (when php is running as a CGI application.) Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-993904 Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 Yes. It tells the server that session's in use. setcookie's third parameter is for setting expiration time. How would that work? setcookie(session_name(), session_id(), time() + 86400); Like this? Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994098 Share on other sites More sharing options...
Mchl Posted January 13, 2010 Share Posted January 13, 2010 expire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes). Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally. expire is compared to the client's time which can differ from server's time. Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994115 Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 That's not what I meant the OP asked for how to increase the session lifetime you adviced setcookie I'm asking if it possible to extend the session cookie lifetime using this function? Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994116 Share on other sites More sharing options...
Mchl Posted January 13, 2010 Share Posted January 13, 2010 I doubt it. Session's cookie is set by session_start(). I don't know if you can affect it with setcookie(). I guess you should try and see Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994136 Share on other sites More sharing options...
oni-kun Posted January 13, 2010 Share Posted January 13, 2010 I doubt it. Session's cookie is set by session_start(). I don't know if you can affect it with setcookie(). I guess you should try and see Why did you mention setcookie's expiration time then? Did you tl;dr PFM? Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994159 Share on other sites More sharing options...
Mchl Posted January 13, 2010 Share Posted January 13, 2010 The OP asked about 'the cookie' without specifying it's supposed to be session's cookie. Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994170 Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 I doubt it. Session's cookie is set by session_start(). I don't know if you can affect it with setcookie(). I guess you should try and see Actually I think this could work as is session_set_cookie_params not just a proxy for setcookie? Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994211 Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 I doubt it. Session's cookie is set by session_start(). I don't know if you can affect it with setcookie(). I guess you should try and see Actually I think this could work as is session_set_cookie_params not just a proxy for setcookie? Edit: Works! session_start(); if (!isset($_SESSION['setcookie'])) { setcookie(session_name(), session_id(), time() + 86400); $_SESSION['setcookie'] = mt_rand(); } else { print $_SESSION['setcookie']; } Ran this script showed nothing (2 cookies were set with same id one to expire upon browser close and one tommorow), closed the browser, open it again ran the script displayed a random number (1 cookie was set to expire tommorow) Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994216 Share on other sites More sharing options...
Mchl Posted January 13, 2010 Share Posted January 13, 2010 Hmm... I don't think so. It doesn't set the cookie, just sets the values for session_start() to use. Re Edit: Cool! Good work! Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994218 Share on other sites More sharing options...
aeroswat Posted January 13, 2010 Author Share Posted January 13, 2010 I doubt it. Session's cookie is set by session_start(). I don't know if you can affect it with setcookie(). I guess you should try and see Actually I think this could work as is session_set_cookie_params not just a proxy for setcookie? Edit: Works! session_start(); if (!isset($_SESSION['setcookie'])) { setcookie(session_name(), session_id(), time() + 86400); $_SESSION['setcookie'] = mt_rand(); } else { print $_SESSION['setcookie']; } Ran this script showed nothing (2 cookies were set with same id one to expire upon browser close and one tommorow), closed the browser, open it again ran the script displayed a random number (1 cookie was set to expire tommorow) Sweet! Thanks much to everyone that helped out! Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994224 Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 Hmm... I don't think so. It doesn't set the cookie, just sets the values for session_start() to use. Re Edit: Cool! Good work! Is this a bug? Because if I request session_get_cookie_params it says: 79623764Array ( [lifetime] => 0 [path] => / [domain] => [secure] => [httponly] => ) Using this code: session_start(); if (!isset($_SESSION['setcookie'])) { setcookie(session_name(), session_id(), time() + 86400); $_SESSION['setcookie'] = mt_rand(); } else { print $_SESSION['setcookie']; print_r(session_get_cookie_params()); } lifetime 0 would imply the session should close upon browser close.. Altough this may aswell be able to work because the garbage collector has not yet cleaned the session. This last makes most sense and is most plausible. PFMaBiSmAd here or salathe for the correct reason why this works? Quote Link to comment https://forums.phpfreaks.com/topic/188255-longer-sessions/#findComment-994231 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.