Understanding Cookies in PHP

A cookie in PHP is a small file that holds data up to 4 KB. Cookies are stored on the client computer.

The syntax for set cookie() is as follows:

set cookie (name, value, expire, path, domain, secure, HTTP only);

  • name: The name of the cookie.
  • value: The value to be stored in the cookie.
  • expire: The expiration time (in seconds from the current time).
  • path: The path within the domain where the cookie will be available.
  • domain: The domain for which the cookie is available.
  • secure: If true, the cookie will only be sent over secure HTTPS connections.
  • HTTP only: If true, the cookie will be accessible only through the HTTP protocol (inaccessible via JavaScript).

 

Follow the steps below to create and use Cookies in PHP

Set a Cookie in PHP :

To set a cookie, we use the setCookie ()  function. This function must be called before any HTML output on the page. It accepts four parameters (name, value, expire, path). 

Example: In this example, we create a cookie named username 

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Set a cookie named "username" with a value of "JohnDoe" that expires in 1 hour
setcookie("username", "Deepak", time() + 3600, "/");
echo "Cookie has been set!";
?>
<?php // Set a cookie named "username" with a value of "JohnDoe" that expires in 1 hour setcookie("username", "Deepak", time() + 3600, "/"); echo "Cookie has been set!"; ?>
<?php 

// Set a cookie named "username" with a value of "JohnDoe" that expires in 1 hour 

setcookie("username", "Deepak", time() + 3600, "/"); 

echo "Cookie has been set!"; 

?>

In this example: 

  • “username” is the cookie name. 
  • “Deepak” is the value assigned to the cookie. 
  • time() + 3600 sets the cookie to expire in one hour (3600 seconds from the current time). 
  • The “/” specifies that the cookie is available throughout the entire website. 

 Accessing Cookies in PHP 

To access cookies, we use $_COOKIE super global array 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
if(isset($_COOKIE["username"])) {
echo "Welcome " . $_COOKIE["username"];
} else {
echo "Cookie 'username' is not set.";
}
?>
<?php if(isset($_COOKIE["username"])) { echo "Welcome " . $_COOKIE["username"]; } else { echo "Cookie 'username' is not set."; } ?>
<?php 

if(isset($_COOKIE["username"])) { 

    echo "Welcome " . $_COOKIE["username"]; 

} else { 

    echo "Cookie 'username' is not set."; 

} 

?>

In the above example, we use isset to check if the cookie exists or not. 

 Deleting a Cookie 

To delete a cookie, set the cookie’s expiration time to a time

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Set the expiration time to one hour ago
setcookie("username", "", time() - 3600, "/");
echo "Cookie 'username' has been deleted!";
?>
<?php // Set the expiration time to one hour ago setcookie("username", "", time() - 3600, "/"); echo "Cookie 'username' has been deleted!"; ?>
<?php 

// Set the expiration time to one hour ago 

setcookie("username", "", time() - 3600, "/"); 

echo "Cookie 'username' has been deleted!"; 

?>

Read Also:

How to Create a PHP Session

Understanding the_content Filter Hook in WordPress: Customizing Post Content

Also Visit:
https://inimisttech.com/

 

 

 

Leave a Reply