PHP Cookies

PHP Cookies

In PHP, cookies are used to store small pieces of data on the user's browser. These data can be retrieved later and used for various purposes like tracking user preferences, sessions, or remembering login information.


What are Cookies?

  • Cookies are small files that are stored on the user's device by the web browser.
  • Cookies can store key-value pairs of data and have attributes like expiration time, domain, path, and security options.
  • Cookies are sent with every HTTP request to the server until they expire.

Create Cookies With PHP


To set a cookie in PHP, you use the setcookie() function. Here's the basic syntax:


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


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


PHP Create/Retrieve a Cookie


To create a cookie, use the setcookie() function. Here's a simple example:

<?php
$cookie_name = "user";
$cookie_value = "JohnDoe";
// Create a cookie that stores a user's name with a 1-hour expiration time
setcookie($cookie_name, $cookie_value, time() + 3600, "/"); // "user" is the cookie name, "JohnDoe" is the value, expires in 1 hour
?>
<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>

Note

The setcookie() function must appear BEFORE the <html> tag.

Modify a Cookie Value


In PHP, you cannot directly modify an existing cookie.


However, you can overwrite an existing cookie by setting a new cookie with the same name and updated value.


Essentially, you are creating a new cookie with the same name but with the modified value.

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
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>

Delete a Cookie


To delete a cookie, you need to set its expiration time to a past date. This will tell the browser to delete the cookie.

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>

Check if Cookies are Enabled


To check if cookies are enabled in the user's browser using PHP, you can use a simple technique that involves setting a test cookie and then checking if it was successfully set.


If the cookie is set correctly, it means cookies are enabled.

<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.