PHP: Read a browser cookie value

Contributor Icon Contributed by qmchenry Date Icon July 14, 2006  
Tag Icon Tagged: PHP programming

Once your web application has set a cookie in a user’s browser, it is a simple matter to retrieve that value in subsequent page requests. This recipe describes reading a value from a cookie in PHP.


After a cookie with a name of ‘user’ has been set in a previous page, the value can be accessed with the global array $_COOKIE like this:

$user = $_COOKIE['user'];

Since this is a normal array, anything you can do to an array like iterate through it one value at a time. For debugging purposes, it is often useful to use this quick trick to see the whole contents of an array:

print_r($_COOKIE);

Previous recipe | Next recipe |
 
blog comments powered by Disqus