Cookie in PHP


Prev Tutorial Next Tutorial

Cookie in PHP

Cookie is generally used to identify a user. Cookies are text files stored on the client computer and they are kept of use tracking purpose. PHP transparently supports HTTP cookies. Using PHP, you can both create and retrieve cookie values.

Note: PHP Cookie must be used before <html> tag.

cookie in php

There are three steps involved in identifying returning users;

  • Server script sends a set of cookies to the browser. For example name, age, or identification number etc.
  • Browser stores this information on local machine for future use.
  • When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.

Create Cookies in PHP

A cookie is created in php using setcookie() function. Here only the name parameter is required. All other parameters are optional.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

Create/Retrieve a Cookie in PHP

The following example creates a cookie named "user" with the value "Hitesh Kumar". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).

We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:

Create Cookie

<?php
$cookie_name = "user";
$cookie_value = "Hitesh Kumar";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(!isset($_COOKIE[$cookie_name]))
{
 echo "Cookie named '" . $cookie_name . "' is not set!";
} else 
{
 echo "Cookie '" . $cookie_name . "' is set!
"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <body> <html>

Modify a Cookie Value

To modify a cookie, just set (again) the cookie using the setcookie() function

Modify Cookie

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(!isset($_COOKIE[$cookie_name]))
{
  echo "Cookie named '" . $cookie_name . "' is not set!";
} 
else 
{
 echo "Cookie '" . $cookie_name . "' is set!
"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <body> <html>

Delete a Cookie in PHP

Delete Cookie

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>

<!DOCTYPE html>
<html>
<body>
<?php

echo "Cookie 'user' is deleted.";

?>
<body>
<html>

Check Cookies are Enabled or not

To Check Cookies are Enabled or not, First Create a test cookie with the setcookie() function, then count the $_COOKIE array variable;

Check Cookie Enabled ?

<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>

<!DOCTYPE html>
<html>
<body>
<?php

if(count($_COOKIE) > 0)
{
 echo "Cookies are enabled.";
} else 
{
 echo "Cookies are disabled.";
}

?>
<body>
<html>

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements