Something that has taken me some time to debug: session_set_cookie_params() does not work when the domain param is just a one level domain, like it was a TLD.
I have a site in an intranet and our internal domain is .local, so trying to set the cookie session to the .local domain does not work:
session_set_cookie_params(0, '/', '.local'); // Does not work
In all test I've done, setting the domain only works for SLDs and above:
session_set_cookie_params(0 , '/', '.sld.local'); Does work
This is nothing to do with PHP but the http protocol, witch does not permit setting cookies for TLDs for obvious security reasons.
session_set_cookie_params
jordi at jcanals dot net
15-Nov-2004 02:39
15-Nov-2004 02:39
shrockc at inhsNO dot SPAMorg
19-Jun-2002 06:19
19-Jun-2002 06:19
when setting the path that the cookie is valid for, always remember to have that trailing '/'.
CORRECT:
session_set_cookie_params (0, '/yourpath/');
INCORRECT:
session_set_cookie_params (0, '/yourpath');
no comment on how long it took me to realize that this was the cause of my authentication/session problems...
gavin_spam at skypaint dot com
26-Feb-2002 11:58
26-Feb-2002 11:58
The first argument to session_set_cookie_params is the number of seconds in the future (based on the server's current time) that the session will expire. So if you want your sessions to last 100 days:
$expireTime = 60*60*24*100; // 100 days
session_set_cookie_params($expireTime);
I was using time()+$expireTime, which is WRONG (a lot of the session_set_cookie_params() examples I found get this wrong, but probably don't care because they are just doing "infinite" sessions).
php at mike2k dot com
09-May-2001 11:16
09-May-2001 11:16
[Editor's Note:
Rasmus' Solution from the PHP-General list:
Just use a session cookie (by not providing an expiry time) and add the
server's expiry timestamp to the value of the cookie. Then when you get
that cookie sent to you, check it against your server's time and make the
decision on whether to accept the cookie or not based on that.
That way you are immune from people not having their system clocks set
right.
-Rasmus
--zak@php.net]
A couple things I noticed when using this. I think it only works if you set the session_set_cookie_params() function BEFORE the session_start() function.
Also, when you set the "lifetime" on the cookie, it takes the seconds offset from the SERVER. it sends the cookie encoded to timeout at the SERVER time. So if your server is +2 minutes ahead of the client, and you set the cookie to timeout after 30 seconds, the client actually has 2 minutes and 30 seconds before the cookie times out. I don't know if there's any way that this can be patched in future versions, and the only alternative I think is setting cookies in javascript, which is hardly the point when using all these specific session functions.