PHP: Read a browser cookie value

Home -> Programming -> Web -> PHP

9497 views

From the computer of: qmchenry (338 recipes)
Created: Jul 14, 2006


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

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);

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  PHP: Delete or expire a browser cookie
  PHP: Set or create a simple browser cookie with setcookie
  Redirect to another web page using PHP
  PHP: Generate Dynamic Images from Text
  Make Wordpress Stop Replacing Double Dash with Em-dash
  Solve PHP error: Cannot modify header information - headers already sent
  PHP: Delete or unlink a file on the server
  PHP syntax: sort an associative array by value with asort
  PHP: Break apart strings with explode
  PHP conditional syntax: greater than

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.