manual/en/features.cookies.php

ARCHIVED 2013-03-19, DATE APPROXIMATE · VERSION 20130319_rev01 · COMPARED WITH 20130102_rev01

Full text changes — 20130102_rev01 to 20130319_rev01

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

12
14
22
3[**_ingen at stocken.ws_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
3[**_ingen at stocken.ws_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
44
55**6 years ago**
66
77`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)*.`
88
99`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.`
2323`If you need a javascript for md5: [http://pajhome.org.uk/crypt/md5/md5src.html](http://pajhome.org.uk/crypt/md5/md5src.html)`
2424
2525`---`
2626
2727`* 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.`
2828
291
292
3030
31[**_mega-squall at caramail dot com_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
31[**_mega-squall at caramail dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
3232
33**7 years ago**
33**8 years ago**
3434
3535`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.`
3636
3737`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 !`
3838
3939`IMPORTANT NOTE : This disables the ability of using the back button if you send the session ID via POST or GET.`
4040
41411
4242
43[**_myfirstname at braincell dot cx_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
43[**_bmorency at jbmlogic dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
4444
45**7 years ago**
46
47`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:`
48
49`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).`
50
51`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.`
52
53`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.`
54
551
56
57[**_myfirstname at braincell dot cx_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
58
4559**9 years ago**
4660
4761`[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]`
4862
4963`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.`
5064
5165`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.`
5266
5367`Stuart Livings`
5468
55\-1
690
5670
57[**_James Olsen_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
71[**_James Olsen_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
5872
59**7 years ago**
73**8 years ago**
6074
6175`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.`
6276
630
77\-1
6478
65[**_kalla\_durga at gmail dot com_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
79[**_john at host89 dot net_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
6680
67**6 years ago**
81**3 years ago**
6882
69`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:`
83`I'm currently developing a secure system utilizing PHP session cookies, and rather than trying to deal with recreating a session every time the script runs (which would hurt server performance), I'm having the script send a 2nd cookie to the client which contains an MD5 of their username along with a random value (so that it's harder to generate a matching key if somebody is trying to hijack the session.) The original random value can be stored as $_SESSION['rndvalue'] or something of the like, and easily re-hashed and compared to the cookie. If it isn't valid, just a simple session_destroy(); does the trick. For higher security, the random value could even be changed at every new page, and to make leeway for that double-click phenomon, save the old one as 'rndvalueold' with the expiration time as 'rndvalueexpire' or something. This will also let users use the back button even if the session ID is passed via GET or POST.`
7084
71`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).`
85\-1
7286
73`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.`
87[**_kalla\_durga at gmail dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
7488
75`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.`
76
770
78
79[**_bmorency at jbmlogic dot com_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
80
8189**7 years ago**
8290
8391`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:`
8492
8593`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).`
8694
8795`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.`
8896
8997`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.`
9098
91\-1
92
93[**_john at host89 dot net_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
94
95**3 years ago**
96
97`I'm currently developing a secure system utilizing PHP session cookies, and rather than trying to deal with recreating a session every time the script runs (which would hurt server performance), I'm having the script send a 2nd cookie to the client which contains an MD5 of their username along with a random value (so that it's harder to generate a matching key if somebody is trying to hijack the session.) The original random value can be stored as $_SESSION['rndvalue'] or something of the like, and easily re-hashed and compared to the cookie. If it isn't valid, just a simple session_destroy(); does the trick. For higher security, the random value could even be changed at every new page, and to make leeway for that double-click phenomon, save the old one as 'rndvalueold' with the expiration time as 'rndvalueexpire' or something. This will also let users use the back button even if the session ID is passed via GET or POST.`
98
9999\-2
100100
101[**_Henry_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php)
101[**_Henry_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php)
102102
103103**3 years ago**
104104
105105`It is better to note not to attach your cookies to and IP and block the IP if it is different as some people use Portable Browsers which will remember the cookies. It is better to show a login screen instead if the IP does not correspond to the session cookie's IP.`