PHP Cookies



PHP Cookies

Cookies are small text file stored on client computer, used to identify a user.

Cookies store a user information like username and password on client computer for further use.
User use same computer many time each time it will send a cookie too.
In PHP we can create a cookie and retrieve cookie values.


Create Cookies with PHP

  • The setcookie() function used to create a cookie.
  • A single domain has maximum 20 cookies.
  • A single cookie can not exceed 4 kilobytes in size.
  • PHP setcookie() function must be called before <html> tag.
  • The setcookie() function has up to six arguments.

setcookie() function syntax:

setcookie(name, value, expire, path, domain, security);

Name : This is name of the cookie and it is used to retrieve cookie.
Value : information or value to be stored in cookie.
Expire : used to set time of expiry of cookie. If not set expiry the cookie automatically expire when browser closed.
Path : “/” means the cookie available in entire website or all directories. or we can specify the directories for valid cookie.
Domain : used to specify the domain name.
Security : used to security purpose. set 1 or 0, 1 for secure transmission using HTTPS and 0 for sent by simple HTTP.

Example of create cookies

setcookie(“example”, “meera”, time() + 3600, “/”, “”, 0);

Here, we create cookie named “example” with value “meera”. The cookie will expire after 1 hour. The “/” means cookie available in all directories.

PHP Example :

Create php form with named “example.php” for create cookie name and id.

<?php
setcookie("name","meera", time() + 3600, "/", "", 0);
setcookie("id", "25", time() + 3600, "/", "", 0);
?>
<HTML>
<head>
<title>Create cookies in PHP</title>
</head>
<body>

<?php
echo "The cookies created for name and id.";
?>
</body>
</HTML>

Retrieve cookies in PHP

The cookies retrieve using the $_COOKIE[] global variable.
The isset() function used to check if a cookie is set or not.

Example  if retrieve cookies in PHP.

Create php form “cexample.php” with below code for retrieve cookie name and id.

<HTML>
<head>
<title>Retrieve cookies in PHP</title>
</head>
<body>

<?php
if(isset($_COOKIE["name"]) && isset($_COOKIE["id"]))
{
echo " The name = " .$_COOKIE["name"]. "<br/>";
echo "The id = ". $_COOKIE["id"];
}
else
{
echo "Sorry !! cookies is not set.";
}

?>
</body>
</HTML>

In above example out put is :

The name = meera
The id = 25


Delete / Destroy cookies in PHP

For delete a cookie, use setcookie() function with date that already expired.

Create php form “dexample.php” for delete cookie name and id.

<?php
setcookie("name","", time() - 3600, "/", "", 0);
setcookie("id", "", time() - 3600, "/", "", 0);
?>
<HTML>
<head>
<title>Delete cookies in PHP</title>
</head>
<body>

<?php
echo "The cookies Deleted for name and id.";
?>
</body>
</HTML>

PHP cookies create , retrieve and delete example.

PHP  cookies example
PHP cookies example

Leave a Reply

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