Cookies in PHP with Examples [2 Steps]

In this article we are going to cover Cookies in PHP with Examples,What are Cookies in PHP, Create Cookies with PHP, Modify a Cookie value in PHP, Delete a Cookie in PHP

What are Cookies in PHP?

A cookie is a small text file that lets you store a small amount of data (nearly 4KB) on the users computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Create Cookies with PHP

A cookie is created with the setcookie() function. The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set.

Syntax:

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

Only the name parameter is required. All other parameters are optional.

Name: It is used to set the name of the cookie.

Value: It is used to set the value of the cookie.

Expire: It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed.

Path: It is used to specify the path on the server for which the cookie will be available.

Domain: It is used to specify the domain for which the cookie is available.

Secure: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.

The following example creates a cookie named “user” with the value “demo”. The cookie will expire after 30 days. We 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.

Example of create cookie in PHP:

<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "demo";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

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

Output:

Cookie 'user' is set!
Value is: demo
Cookies in PHP with Examples [2 Steps] 1
create_cookie

Modify a Cookie value in PHP:

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

Example of modify a cookie value:

<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "cookie_demo";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
	
<?php
if(!isset($_COOKIE[$cookie_name])) {
     echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
     echo "Cookie '" . $cookie_name . "' is set!<br>";
     echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Output:

Cookie 'user' is set!
Value is: cookie_demo

Cookies in PHP with Examples [2 Steps] 2
modify_cookie

Delete a Cookie in PHP:

To delete a cookie, use the setcookie() function with an expiration date in the past

Example of delete a cookie:

<!DOCTYPE html>
<?php
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>

Output:

Cookie 'user' is deleted.
Cookies in PHP with Examples [2 Steps] 3
cookie_delete

Conclusion:

In this article we have have covered What are Cookies in PHP, Create Cookies with PHP, Modify a Cookie value in PHP, Delete a Cookie in PHP.

Related Articles:

Reference:

Cookies in PHP official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap