manual/en/features.cookies.php
20170101_rev01 · COMPARED WITH 20161107_rev01 · ARCHIVE SNAPSHOT, DATE APPROXIMATE
Full text changes — 20161107_rev01 to 20170101_rev01
| 3 | 3 | Any cookies sent to you from the client will automatically be included into a [$\_COOKIE](http://php.net/manual/en/reserved.variables.cookies.php) auto-global array if [variables\_order](http://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://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. |
| 6 | 6 | |
| 7 | 7 | For more details, including notes on browser bugs, see the [setcookie()](http://php.net/manual/en/function.setcookie.php) and [setrawcookie()](http://php.net/manual/en/function.setrawcookie.php) function. |
| 8 | 8 | |
| 9 | 117 | |
| 9 | 121 | |
| 10 | 10 | |
| 11 | 11 | [**_Tugrul_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 12 | 12 | |
| 13 | 13 | **1 year ago** |
| 14 | 14 | |
| 15 | 15 | `Setting new cookie ============================= <?php setcookie("name","value",time()+$int); /*name is your cookie's name value is cookie's value $int is time of cookie expires*/ ?> Getting Cookie ============================= <?php echo $_COOKIE["your cookie name"]; ?> Updating Cookie ============================= <?php setcookie("color","red"); echo $_COOKIE["color"]; /*color is red*/ /* your codes and functions*/ setcookie("color","blue"); echo $_COOKIE["color"]; /*new color is blue*/ ?> Deleting Cookie ============================== <?php unset($_COOKIE["yourcookie"]); /*Or*/ setcookie("yourcookie","yourvalue",time()-1); /*it expired so it's deleted*/ ?> Reference: [http://gencbilgin.net/php-cookie-kullanimi.html](http://gencbilgin.net/php-cookie-kullanimi.html)` |
| 16 | 16 | |
| 17 | 13 | |
| 17 | 12 | |
| 18 | 18 | |
| 19 | 19 | [**_myfirstname at braincell dot cx_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 20 | 20 | |
| 21 | 21 | **13 years ago** |
| 22 | 22 | |
| 23 | 23 | `[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]` |
| 29 | 29 | `Stuart Livings` |
| 30 | 30 | |
| 31 | 31 | \-19 |
| 32 | 32 | |
| 33 | 33 | [**_ingen at stocken.ws_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 34 | 34 | |
| 35 | **9 years ago** | |
| 35 | **10 years ago** | |
| 36 | 36 | |
| 37 | 37 | `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)*.` |
| 38 | 38 | |
| 39 | 39 | `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.` |
| 40 | 40 | |
| 41 | 41 | `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.` |
| 75 | 75 | `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).` |
| 76 | 76 | |
| 77 | 77 | `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.` |
| 78 | 78 | |
| 79 | 79 | `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.` |
| 80 | 80 | |
| 81 | \-38 | |
| 82 | ||
| 83 | [**_Anonymous_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) | |
| 84 | ||
| 85 | **3 years ago** | |
| 86 | ||
| 87 | `Your note is too short. Trying to test the notes system? Save us the trouble of deleting your test, and don't. It works.` | |
| 88 | ||
| 89 | 81 | \-42 |
| 90 | 82 | |
| 91 | 83 | [**_kalla\_durga at gmail dot com_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 92 | 84 | |
| 93 | 85 | **10 years ago** |
| 94 | 86 | |
| 97 | 89 | `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).` |
| 98 | 90 | |
| 99 | 91 | `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.` |
| 100 | 92 | |
| 101 | 93 | `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.` |
| 102 | 94 | |
| 103 | \-41 | |
| 95 | \-42 | |
| 104 | 96 | |
| 105 | 97 | [**_mega-squall at caramail dot com_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 106 | 98 | |
| 107 | 99 | **11 years ago** |
| 108 | 100 | |
| 109 | 101 | `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.` |