manual/en/features.cookies.php
20140312_rev01 · COMPARED WITH 20140208_rev01 · ARCHIVE SNAPSHOT, DATE APPROXIMATE
Full text changes — 20140208_rev01 to 20140312_rev01
| 3 | 3 | Any cookies sent to you from the client will automatically be included into a [$\_COOKIE](http://www.php.net/manual/en/reserved.variables.cookies.php) auto-global array if [variables\_order](http://www.php.net/manual/en/ini.core.php) contains "C". If you wish to assign multiple values to a single cookie, just add _\[\]_ to the cookie name. |
| 4 | 4 | |
| 5 | 5 | Depending on [register\_globals](http://www.php.net/manual/en/ini.core.php), regular PHP variables can be created from cookies. However it's not recommended to rely on them as this feature is often turned off for the sake of security. $HTTP\_COOKIE\_VARS is also set in earlier versions of PHP when the [track\_vars](http://www.php.net/manual/en/ini.core.php) configuration variable is set. (This setting is always on since PHP 4.0.3.) |
| 6 | 6 | |
| 7 | 7 | For 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. |
| 8 | 8 | |
| 9 | 20 | |
| 9 | 16 | |
| 10 | 10 | |
| 11 | 11 | [**_ingen at stocken.ws_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) |
| 12 | 12 | |
| 13 | 13 | **7 years ago** |
| 14 | 14 | |
| 15 | 15 | `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)*.` |
| 31 | 31 | `If you need a javascript for md5: [http://pajhome.org.uk/crypt/md5/md5src.html](http://pajhome.org.uk/crypt/md5/md5src.html)` |
| 32 | 32 | |
| 33 | 33 | `---` |
| 34 | 34 | |
| 35 | 35 | `* 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.` |
| 36 | 36 | |
| 37 | 7 | |
| 37 | 6 | |
| 38 | 38 | |
| 39 | 39 | [**_myfirstname at braincell dot cx_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) |
| 40 | 40 | |
| 41 | 41 | **10 years ago** |
| 42 | 42 | |
| 43 | 43 | `[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]` |
| 47 | 47 | `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.` |
| 48 | 48 | |
| 49 | 49 | `Stuart Livings` |
| 50 | 50 | |
| 51 | 51 | 1 |
| 52 | 52 | |
| 53 | [**_mega-squall at caramail dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) | |
| 53 | [**_Henry_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) | |
| 54 | 54 | |
| 55 | **8 years ago** | |
| 55 | **4 years ago** | |
| 56 | 56 | |
| 57 | `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.` | |
| 57 | `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.` | |
| 58 | 58 | |
| 59 | `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 !` | |
| 59 | \-1 | |
| 60 | 60 | |
| 61 | `IMPORTANT NOTE : This disables the ability of using the back button if you send the session ID via POST or GET.` | |
| 61 | [**_Anonymous_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) | |
| 62 | 62 | |
| 63 | 0 | |
| 63 | **10 months ago** | |
| 64 | 64 | |
| 65 | [**_Henry_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) | |
| 65 | `Your note is too short. Trying to test the notes system? Save us the trouble of deleting your test, and don't. It works.` | |
| 66 | 66 | |
| 67 | **4 years ago** | |
| 67 | \-2 | |
| 68 | 68 | |
| 69 | `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.` | |
| 69 | [**_mega-squall at caramail dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) | |
| 70 | 70 | |
| 71 | \-2 | |
| 71 | **9 years ago** | |
| 72 | 72 | |
| 73 | [**_Anonymous_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) | |
| 73 | `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.` | |
| 74 | 74 | |
| 75 | **9 months ago** | |
| 75 | `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 !` | |
| 76 | 76 | |
| 77 | `Your note is too short. Trying to test the notes system? Save us the trouble of deleting your test, and don't. It works.` | |
| 77 | `IMPORTANT NOTE : This disables the ability of using the back button if you send the session ID via POST or GET.` | |
| 78 | 78 | |
| 79 | \-4 | |
| 79 | \-5 | |
| 80 | 80 | |
| 81 | 81 | [**_kalla\_durga at gmail dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) |
| 82 | 82 | |
| 83 | 83 | **8 years ago** |
| 84 | 84 | |
| 85 | 85 | `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:` |
| 87 | 87 | `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).` |
| 88 | 88 | |
| 89 | 89 | `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.` |
| 90 | 90 | |
| 91 | 91 | `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.` |
| 92 | 92 | |
| 93 | \-4 | |
| 93 | \-6 | |
| 94 | 94 | |
| 95 | 95 | [**_bmorency at jbmlogic dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) |
| 96 | 96 | |
| 97 | 97 | **8 years ago** |
| 98 | 98 | |
| 99 | 99 | `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:` |
| 101 | 101 | `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).` |
| 102 | 102 | |
| 103 | 103 | `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.` |
| 104 | 104 | |
| 105 | 105 | `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.` |
| 106 | 106 | |
| 107 | \-10 | |
| 107 | \-15 | |
| 108 | 108 | |
| 109 | 109 | [**_meetyashah at gmail dot com_**](http://www.php.net/manual/en/features.cookies.php) [¶](http://www.php.net/manual/en/features.cookies.php) |
| 110 | 110 | |
| 111 | **9 months ago** | |
| 111 | **10 months ago** | |
| 112 | 112 | |
| 113 | 113 | `PAGE 1` |
| 114 | 114 | |
| 115 | 115 | `<?php echo $_COOKIE["first"]; ?> PAGE 2` |
| 116 | 116 | |
| 117 | 117 | `<?php if(isset($_COOKIE["first"])) { echo $_COOKIE["first"];} echo '<br />'; if(isset($_COOKIE["second"])){ echo $_COOKIE["second"]; } echo '<br />'; if(isset($_COOKIE["third"])){ echo $_COOKIE["third"]; } ?>` |