codescracker


php

PHP Cookies



« Previous Tutorial Next Tutorial »


A cookie in PHP is a data stored on user's computer.

Cookie's data is sent from a website and stored on local computer or user's computer by web browser of that local or user's computer.

Cookie's data is used just for identification purpose for each and every users and to provide little better user experience when browsing the same website next time.

Create Cookie in PHP

To create cookie in PHP, use setcookie() function of PHP.

Here is the syntax or general form used to create cookie in PHP. Following syntax to create cookie only includes essential parameter which is cookie name.

setcookie(cookie_name);

Now the below syntax includes all the parameters used in creating cookie in PHP.

setcookie(cookieName, cookieValue, cookieExpire, cookiePath, cookieDomain, cookieSecure, cookieHttponly);

Except cookieName, all the other parameters used to create cookie in PHP are optional. Only cookieName is the parameter which is compulsory to create a new cookie in PHP.

PHP Create Cookie Example

Here is an example illustrating how to create cookie in PHP.

<!DOCTYPE html>
<?php
	$cookieName = "codescracker";
	$cookieValue = "Computer Programmer";
	// set cookie for 30 days with above name and value
	setcookie($cookieName, $cookieValue, time() + (86400 * 30));
	// 86400 seconds = 1 day
	// 1 * 30 = 30 days
?>
<html>
<head>
	<title>Create Cookie Example in PHP</title>
</head>
<body>
<?php 
	if(!isset($_COOKIE[$cookieName]))
	{
		echo "Creating cookie....";
	}
	else 
	{
		echo "Cookie created.";
	}
?>
</body>
</html>

Here is the sample output of the above creating cookie example code in PHP. This is the screenshot of the sample output at first run of the above PHP program in web browser:

create cookie in php

Now if you run the above creating cookie program in PHP at first time, the cookie with name codescracker and value Computer Programmer is created for 30 days. Therefore if you re-run the same program or refresh your browser for same program, then here you will see the following output this time.

php create cookie

If you create the above cookie only for 2 seconds, then you will see that your cookie will be created and expire in only 2 seconds. After creating your cookie for 2 seconds, if you refresh your browser after 2 seconds then you will see the message Creating cookie.... after 2 seconds of created cookie on running the above program used in creating cookie in PHP.

From the above statement, you can realize that how much the time is important in setting or creating cookie in PHP.

PHP Retrieve Cookie Example

Here is an example shows how to retrieve cookie in PHP.

<!DOCTYPE html>
<?php
	$cookieName = "codescracker";
	if(!isset($_COOKIE[$cookieName]))
	{
		$cookieValue = "Computer Programmer";
		setcookie($cookieName, $cookieValue, time() + (86400 * 30));
	}
?>
<html>
<head>
	<title>Retrieve Cookie Example in PHP</title>
</head>
<body>
<?php 
	if(isset($_COOKIE[$cookieName]))
	{
		echo "Cookie is retrieving...<br/>";
		echo "Cookie is retrieved successfully...<br/><br/>";
		echo "Name of the cookie = ".$cookieName."<br/>";
		echo "Value of the cookie = ".$_COOKIE[$cookieName]."<br/>";
	}
	else 
	{
		echo "Error occurred...<br/>";
		echo "exiting...<br/>";
	}
?>
</body>
</html>

Here is the sample output produced by the above retrieving cookie example code in PHP

php retrieve cookie

PHP Check Cookie is Set or Not

To check whether cookie is set or not in PHP, use isset() function on the $_COOKIE, a superglobal variable. Let's take an example on checking that cookie is already set or not.

<html>
<head>
	<title>Checking cookie is set or not in PHP</title>
</head>
<body>
<?php
	// this php example check whether the cookie 
	// is set or not - CodesCracker
	// use isset to check that cookie is set 
	// or not set
	$count = 0;
	if(isset($_COOKIE['cookieName']))
	{
		// cookie is set
		echo "Cookie is set<br/>";
		echo "You can proceed!";
		$count++;
	}
	else 
	{
		// cookie is not set
		echo "Cookie is not set<br/>";
		echo "You can't proceed!";
	}
	if($count==1)
	{
		echo "<br/>You are ".$_COOKIE['cookieName'];
	}
?>
</body>
</html>

As the cookie is not set, therefore here is the sample output produced by the above cookie example code in PHP.

php check cookie set

Now try another example to check for cookie whether it is set or not on user's computer in PHP.

<html>
<head>
	<title>Checking cookie is set or not in PHP</title>
</head>
<body>
<?php
	$cookieName = "codescracker";
	$count = 0;
	if(isset($_COOKIE[$cookieName]))
	{
		echo "Cookie is set<br/>";
		echo "You can proceed!";
		$count++;
	}
	else 
	{
		echo "Cookie is not set<br/>";
		echo "You can't proceed!";
	}
	if($count==1)
	{
		echo "<br/>You are ".$_COOKIE[$cookieName];
	}
?>
</body>
</html>

As the cookie named codescracker is set from the creating cookie example program, therefore this time the output of the above checking cookie is set or not example code in PHP will be.

php cookie

PHP Delete Cookie

To delete any cookie in PHP, just create the cookie again with same name, but set the expiration time as 1 hour back or ago, that is set time as time()-3600.

Here is an example shows how to delete cookie in PHP from user's computer.

<html>
<head>
	<title>Delete Cookie Example in PHP</title>
</head>
<body>
<?php
	// deleting cookie example in PHP - CodesCracker
	echo "Deleting the cookie <b>codescracker</b>....<br/>";
	// To delete cookie in PHP, just set/create the 
	// cookie with expiration time as 1 hour ago/back
	// using the same name
	setcookie("codescracker", "", time()-3600);
	echo "The cookie is deleted successfully.";
?>
</body>
</html>

Here is the sample output produced by the above deleting cookie example code in PHP.

php delete cookie

PHP Check Cookie is Deleted or Not

Here is an example shows how to check whether the cookie is deleted or not in PHP.

<html>
<head>
	<title>Check Cookie is deleted or not Example in PHP</title>
</head>
<body>
<?php
	$cookieName = "codescracker";
	if(isset($_COOKIE[$cookieName]))
	{
		echo "The cookie <b>$cookieName</b> is not deleted.";
	}
	else 
	{
		echo "The cookie <b>$cookieName</b> is deleted.";
	}
?>
</body>
</html>

Here is the sample output produced by the above PHP example program used to check whether the cookie is deleted or not.

php cookie example

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test