PHP Cookies


What is a Cookie?

A cookie is a little piece of data that is used to identify a person (from a specific device).

Cookie is a small file that the server stores on the computer of the user.

The cookie will be sent each time the same computer requests a page using a browser.

Cookie values can be created and retrieved with PHP.

You can also search for these topics, cookies in php, how to create cookies in php, store the cookies in php.

PHP Create/Retrieve a Cookie

The setcookie() function is used to creates a cookie.

The $_COOKIE is a global variable and used to retrieve an existing cookie.

The isset() function is used to check whether or not the cookie has been set or created.

Syntax :-

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

The name argument is the only one that required. The rest of the parameters are purely optional.

Example :- The example below produces a "user" cookie of the "abc" value. After 30 days (86400 * 30), the cookie expires. The "/" indicates that the cookie is present on the whole site (otherwise, select the directory you prefer) :

<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "abc";
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 named 'user' is not set!

Note: You might have to reload the page to see the value of the cookie.

Output:- After refresh the page

Cookie 'user' is set!
Value is: abc

Note :- The setcookie() function must occur before the html tag, or before sending any text to the browser screen.

When delivering the cookie, the value is automatically URLencoded, and when receiving the cookie, it is automatically decoded (to avoid URLencoding, use setrawcookie() instead).

You can also search for these topics, php create cookies, php create persistent cookies, php create cookies session, php setcookie() function, syntax for php create cookies, php create cookies parameter, php retrieve a cookie, php create cookie example, php retrieve a cookie example, check php cookies enable or not, php cookie enable or not example, php cookies set, php isset() function, how to find out if the cookie is set, syntax for php create a cookies, syntax for php retrieve a cookies.

Modify a Cookie Value

The setcookie() function is also used to change or modify a exiting cookie value.

Example :-

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

if(isset($_COOKIE[$cookie_name])) {
     echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

Output :-

Value is: xyz

Note: You might have to reload the page to see the new value of the cookie.

You can also search for these topics, php modify a cookie value, cookie value to modify using php, alter a cookie value in php, how to change a cookie value in php, example for modify a cookie value in php.

Delete a Cookie

The setcookie() function is also used to remove an existing cookie by setting an expiration date in the past.

Example :- We are deleting the "user" cookie by setting time one hour ago.

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

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

Output :-

Cookie 'user' is deleted.

You can also search for these topics, php delete a cookie value, cookie value to delete using php, destroy a cookie value in php, how to remove a cookie value in php, example for delete a cookie in php.

Check if Cookies are Enabled

The following instance produces a simple script that verifies the activation of cookies. Try creating a test cookie using the setcookie() feature and then count the array variable $_COOKIE :

<!DOCTYPE html>
<?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>

Output :-

Cookies are enabled.

You can also search for these topics, php check if cookies are enabled, check if the cookies enabled or not, Example for check if cookies are enabled using php, check for cookies availability.