PHP Reading Cookies in Scripts

Reading cookies in a PHP script is a simple process. In the last post we discussed that a cookie can be set by manipulating HTML header or using setcookie() PHP function. For reading cookies you don’t need to use a header or function for reading cookie.

How is it done??

PHP provides a superglobal  array $_COOKIE. Like other superglobals this is also an associative array. It contains the values against the arguments sent by the server on the client machine using the PHP setting cookies code.  The cookie values are accessed just as you do with an array by giving the name of the cookie as string in square brackets. You can get all the arguments set for this named cookie

Syntax for Reading Cookies in PHP Script

echo $_COOKIE[‘name of the cookie’];

or

var_name=$_COOKIE[‘name of the cookie’];

For example, if you set a cookie to record visits of a user for you website with name userVisits, then to display this cookie value you can use this code in your PHP script.  

echo $_COOKIE[“userVisits”]; 

If you want to update this value in the user’s system, use the setcookie() function to reset its value after incrementing userVisits 

You are aware that $_GET  and  $_POST are used to read values sent from a webpage to a PHP script. You should be careful while reading values from $_COOKIE array in you scripts because a malicious user may use the cookie information to send some harmful data to server. So, the data from $_COOKIE must always be validated and must not be used in the same way we use $_GET  and  $_POST associative arrays.

Cookies set in one page request cannot be read in the same page. If you requested a certain page from the server, the server will first set the cookie. $_COOKIE can get the cookie data only with the next request for same website. First time when the PHP script is executed, server sends the cookie to the browser. The browser saves the cookie in system. This cookie will be returned to server only with the  next request by the user’s browser.

Reading Cookies in PHP Scripts

Example

setcookie( “userVisits”, 0 );  
echo isset( $_COOKIE[“userVisits”] );  

 This above script displays false when it is executed first time as the associative array  $_COOKIE[ “userVisits “ ]  doesn’t  exist. When the page with above scripts is reloaded you get true. The second time browser sent cookie “userVisits “ to the server which is now readable through  $_COOKIE  array.

Updating a cookie

A cookie can be updated by sending the new cookie value with a setcookie() function in the updating script. The updated cookie will not be available until the page request is resent by the browser to the server. This is so because for the current request $_COOKIE array will be holding the old value available before the execution of the current script. When the page is refreshed or request is sent again $_COOKIE array will return the updated value. 

Example Updating and Reading Cookies in PHP Scripts

The following PHP scripts describes how reading cookies and updating is done in a PHP scripts. Here a cookie with name “Visits” is used to store the number of visits of a user in one session. If the cookie is not set “Cookie Not set” is displayed in browser, else the visit count is displayed and the cookie is updated with new count by incrementing the last count returned by $_COOKIE array.

<?php
$cnt=0;
if (isset($_COOKIE["Visits"]))
{
	echo  $_COOKIE["Visits"] ;
	$cnt=$_COOKIE["Visits"] ;
	$cnt=$cnt+1;
}
else
{
	echo  "Cookie Not set";
}
setcookie("Visits", $cnt ); 
?>
After first request by browser, cookie is initialized to 0 to be used in second request

After second request by browser the stored 0 value is displayed and incremented to 1

After third request by browser

After fourth request by browser

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *