- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP Cookies
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:

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.

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 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.

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 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 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.

« Previous Tutorial Next Tutorial »