manual/en/features.cookies.php

ARCHIVED 2009-02-15, DATE APPROXIMATE · VERSION 20090215_rev01 · COMPARED WITH 20080504_rev01

Full text changes — 20080504_rev01 to 20090215_rev01

COLOUR MARKS THE SEVERITY OF A FLAGGED CLAUSE · + AND − MARK ADDED AND REMOVED

77For more details, including notes on browser bugs, see the [setcookie()](http://www.php.net/manual/en/function.setcookie.php) and [setrawcookie()](http://www.php.net/manual/en/function.setrawcookie.php) function.
88
99 [add a note](http://www.php.net/manual/add-note.php) User Contributed Notes
1010**Cookies**
1111
1212**ingen at stocken.ws**
13[19-Nov-2006 12:26](http://www.php.net/manual/en/features.cookies.php)
13[19-Nov-2006 08:26](http://www.php.net/manual/en/features.cookies.php)
1414
1515`If you want a secured session not tied to the client IP you can use the valid-for-one-query method below, but to safeguard against a scenario where the legitimate user clicks twice, you can use a shutdown function (register_shutdown_function)*.`
1616
1717`It will check to see if the script terminated prematurely (connection_aborted), and reset the valid session ID. That way, it's still valid when the user makes the second request. If the script ends properly, the new session ID will be used instead.`
1818
1919`Now, since you can't set a cookie from the shutdown function (after output has been sent), the cookie should contain both the previous valid session ID and the new one. Then the server script will determine (on the next request) which one to use.`
3232
3333`---`
3434
3535`* You could use session_set_save_handler and make sure the session ID is generated in the open function. I haven't done that so I can't make any comments on it yet.`
3636
3737**kalla\_durga at gmail dot com**
38[03-Feb-2006 12:10](http://www.php.net/manual/en/features.cookies.php)
38[03-Feb-2006 08:10](http://www.php.net/manual/en/features.cookies.php)
3939
4040`In response to the solution posted in the comment below, there are some practical issues with this solution that must be kept in mind and handled by your code. I developed an application using a similar "use-it-once" key to manage sessions and it worked great but we got some complaints about legitimate users getting logged out without reasons. Turns out the problem was not tentative highjacking, it was either:`
4141
4242`A- Users double click on links or make 2 clicks very fast. The same key is sent for the 2 clicks because the new key from the first click didn't get to the browser on time for the second one but the session on the server did trash the key for the new one. Thus, the second click causes a termination of the session. (install the LiveHttpHeaders extension on firefox and look at the headers sent when you click twice very fast, you'll see the same cookie sent on both and the new cookie getting back from the server too late).`
4343
4444`B- For any given reason, the server experiences a slow down and the response with the new key (which has replaced the old one on the server) is not returned to the browser fast enough. The user gets tired of waiting and clicks somewhere else. He gets logged out because this second click send the old key which won't match the one you have on your server.`
4545
4646`Our solution was to set up a grace period where the old key was still valid (the current key and the previous key were both kept at all times, we used 15 seconds as a grace period where the old key could still be used). This has the drawback of increasing the window of time for a person to highjack the session but if you tie the validity of the old key to an IP address and/or user agent string, you still get pretty good session security with very very few undesired session termination.`
4747
4848**bmorency at jbmlogic dot com**
49[07-Oct-2005 05:14](http://www.php.net/manual/en/features.cookies.php)
49[07-Oct-2005 12:14](http://www.php.net/manual/en/features.cookies.php)
5050
5151`In response to the solution posted in the comment below, there are some practical issues with this solution that must be kept in mind and handled by your code. I developed an application using a similar "use-it-once" key to manage sessions and it worked great but we got some complaints about legitimate users getting logged out without reasons. Turns out the problem was not tentative highjacking, it was either:`
5252
5353`A- Users double click on links or make 2 clicks very fast. The same key is sent for the 2 clicks because the new key from the first click didn't get to the browser on time for the second one but the session on the server did trash the key for the new one. Thus, the second click causes a termination of the session. (install the LiveHttpHeaders extension on firefox and look at the headers sent when you click twice very fast, you'll see the same cookie sent on both and the new cookie getting back from the server too late).`
5454
5555`B- For any given reason, the server experiences a slow down and the response with the new key (which has replaced the old one on the server) is not returned to the browser fast enough. The user gets tired of waiting and clicks somewhere else. He gets logged out because this second click send the old key which won't match the one you have on your server.`
5656
5757`Our solution was to set up a grace period where the old key was still valid (the current key and the previous key were both kept at all times, we used 15 seconds as a grace period where the old key could still be used). This has the drawback of increasing the window of time for a person to highjack the session but if you tie the validity of the old key to an IP address and/or user agent string, you still get pretty good session security with very very few undesired session termination.`
5858
5959**mega-squall at caramail dot com**
60[23-Feb-2005 01:04](http://www.php.net/manual/en/features.cookies.php)
60[23-Feb-2005 09:04](http://www.php.net/manual/en/features.cookies.php)
6161
6262`I found a solution for protecting session ID without tying them to client's IP. Each session ID gives access for only ONE querry. On the next querry, another session ID is generated and stored. If somebody hacks the cookie (or the session ID), the first one of the user and the pirate that will use the cookie will get the second disconnected, because the session ID has been used.`
6363
6464`If the user gets disconnected, he will reconnect : as my policy is not to have more than one session ID for each user (sessions entries have a UNIQUE key on the collomn in which is stored user login), every entries for that user gets wiped, a new session ID is generated and stored on users dirve : the pirate gets disconnected. This lets the pirate usually just a few seconds to act. The slower visitors are browsing, the longer is the time pirates get for hacking. Also, if users forget to explicitly end their sessions .... some of my users set timeout longer than 20 minutes !`
6565
6666`IMPORTANT NOTE : This disables the ability of using the back button if you send the session ID via POST or GET.`
6767
6868**James Olsen**
69[21-Jan-2005 08:14](http://www.php.net/manual/en/features.cookies.php)
69[22-Jan-2005 04:14](http://www.php.net/manual/en/features.cookies.php)
7070
7171`Tying the session to the IP of the user is not a good idea. Some users, notably AOL users, are behind a rotating proxy which means their hits to the server will actually be coming from different IP addresses over the duration of their visit. Trying the session to the IP will not work properly for those users.`
7272
7373**myfirstname at braincell dot cx**
74[24-Sep-2003 08:47](http://www.php.net/manual/en/features.cookies.php)
74[24-Sep-2003 03:47](http://www.php.net/manual/en/features.cookies.php)
7575
7676`[Editor's note: Wilson's comment has been deleted since it didn't contain much useful information, but this note is preserved although its reference is lost]`
7777
7878`Just a general comment on Wilton's code snippet: It's generally considered very bad practice to store usernames and/or passwords in cookies, whether or not they're obsfucated. Many spyware programs make a point of stealing cookie contents.`
7979
8080`A much better solution would be to either use the PHP built in session handler or create something similar using your own cookie-based session ID. This session ID could be tied to the source IP address or can be timed out as required but since the ID can be expired separately from the authentication criteria the authentication itself is not compromised.`