manual/en/features.cookies.php
20150101_rev01 · COMPARED WITH 20140711_rev01 · ARCHIVE SNAPSHOT, DATE APPROXIMATE
Full text changes — 20140711_rev01 to 20150101_rev01
| 1 | 1 | PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the [setcookie()](http://php.net/manual/en/function.setcookie.php) or [setrawcookie()](http://php.net/manual/en/function.setrawcookie.php) function. Cookies are part of the HTTP header, so [setcookie()](http://php.net/manual/en/function.setcookie.php) must be called before any output is sent to the browser. This is the same limitation that [header()](http://php.net/manual/en/function.header.php) has. You can use the [output buffering functions](http://php.net/manual/en/ref.outcontrol.php) to delay the script output until you have decided whether or not to set any cookies or send any headers. |
| 2 | 2 | |
| 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 | 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. $HTTP\_COOKIE\_VARS is also set in earlier versions of PHP when the [track\_vars](http://php.net/manual/en/ini.core.php) configuration variable is set. (This setting is always on since PHP 4.0.3.) | |
| 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 | 9 | 3 |
| 10 | 10 | |
| 11 | 11 | [**_Henry_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 15 | 15 | `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.` |
| 16 | 16 | |
| 17 | 17 | 4 |
| 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 | **10 years ago** | |
| 21 | **11 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]` |
| 24 | 24 | |
| 25 | 25 | `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.` |
| 26 | 26 | |
| 27 | 27 | `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.` |
| 29 | 29 | `Stuart Livings` |
| 30 | 30 | |
| 31 | 31 | 3 |
| 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 | **7 years ago** | |
| 35 | **8 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.` |
| 77 | 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 | 79 | \-5 |
| 80 | 80 | |
| 81 | 81 | [**_bmorency at jbmlogic dot com_**](http://php.net/manual/en/features.cookies.php) [¶](http://php.net/manual/en/features.cookies.php) |
| 82 | 82 | |
| 83 | **8 years ago** | |
| 83 | **9 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:` |
| 86 | 86 | |
| 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.` |