Cookies In PHP



Cookies are invented to allow the webmasters to store the user information on the user's system, basically it is the concept to store the user name and password on the user's machine so that there is no need to logged in (filling the User name and Password), every time to access the particular website/account. Through the Cookies we can store the other information as well like the name, added produucts into the cart, last visit etc. Cookies are stored on the user's computer in the form of text files. PHP supports HTTP Cookies.

Set Cookies with PHP:

You can set the Cookies by using the predefined PHP function setcookie(), the function requires upto 3 arguments. Remember that the setcookie() should be called before the tag and For each cookie setcookie() has to be called separately.

setcookie(name, value, expiration):

name : It is required and it represents the name of cookie. By which you can use this cookies in future.
value : The value that is stored in your cookie. i.e. user name,password and the last visit (date).
expiration : The date when the cookie will expire and be deleted from the user’r system. If have not set this expiration date, then it will be removed when the browser is restarted. The deafault time of the PHP Cookies is 1440 Seconds.

<?php
	//Calculate 30 days in the future
	//seconds * minutes * hours * days + current time
	$cookiestime = 60 * 60 * 24 * 30 + time(); 
	setcookie('EWAVisit', date("G:i - m/d/y"), $cookiestime); 
?>

In the above example we have created a cookie that measures how often user’s return to visit our webpage. We are here calculating the returning user data for the one months only.

Multi Cookie Setting:

<?php
   setcookie("Username", "EWA", time()+3600);
   setcookie("Userage", "10", time()+3600);
?>
<html>
	<head>
		<title>Cookies In PHP</title>
	</head>
	<body>
		<?php echo "Cookies are set"?>
	</body>
</html>

Retrieving Stored data in Cookies PHP:

For retrieving the PHP Cookie values we can use $_COOKIE or $HTTP_COOKIE_VARS variables. Now I am going to display the cookies values set in above example on the new page.

<html>
	<head>
		<title>Displaying the Cookies values</title>
	</head>
	<body>
		<?php
			echo $_COOKIE["Username"]; // OR you can use $HTTP_COOKIE_VARS["name"]
			echo $_COOKIE["Userage"]; // OR you can use $HTTP_COOKIE_VARS["name"]
		?>
	</body>
</html>

Now we can check by using the isset() that Cookie is set or not, the example is given below:

<html>
	<head>
		<title>Displaying the Cookies values</title>
	</head>
	<body>
		<?php
			  if( isset($_COOKIE["Username"])){
					  echo "Welcome " . $_COOKIE["Username"];
				} else {
			   		 echo "Cookie is not Set";
				}
		?>
	</body>
</html>

Deleting the Cookie values:

By default the cookies are set to be deleted when the browser is clossed, if you want to destroy the stored cookies then the syntax is given below:

<?php
  setcookie( "Username", "", time()- 60);
  setcookie( "Userage", "", time()- 60);
?>

This is all about the PHP Cookies, So keep reading and enjoy the PHP coding.