PHP Cookies
By default PHP is a stateless application means you can not track the activity of a user during the accessing of different web pages of the web site. With the help of the cookie, you can set some variable to track the user state.
PHP cookie is a Super Global and used to set some variable to track the user state or activity.
Caution: Cookie variables are stored in the client or visitor computer, not on the web server.
PHP Setcookie Function to set cookie in PHP
With the setcookie() function you can create a cookie.
Syntax of the setcookie() function
<!DOCTYPE html> <html> <head> </head> <body> setcookie(name, value, expire, path, domain, secure, httponly); </body> </body> </html>
Note:In setcookie() function name parameter is required others parameters are optional.
Example – 1 Code to set and read Cookies
<?php $cookie_name = "visitor"; $cookie_value = "John William"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day ?> <!DOCTYPE html> <html> <head> </head> <body> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "visitor = " . $_COOKIE[$cookie_name]; } ?> </body> </html>
In the above example, we create a cookie of name visitor and the value is John William”
Note:You must have to write setcookie() function code before the starting of HTML tags.
How to Modify a Cookie Value
If you want to modify the value of the cookie again then you have to set the value of cookie through setcookie() function again the value will be modified.
Example – 2 To modify the Cookie Value code
<?php $cookie_name = "visitor"; $cookie_value = "Lariya Ela"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day ?> <!DOCTYPE html> <html> <head> </head> <body> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "visitor = " . $_COOKIE[$cookie_name]; } ?> </body> </html>
In the above example, we create a cookie of name visitor and now I change the value of a cookie with “Lariya Ela”.
How to Delete Cookie in PHP
If you want to delete the setcookie then you have to set the cookie again but the time will be set in negative or past.
Example – 3 Delete Cookie Code in PHP
<?php setcookie("visitor", "", time() - 4500); ?> <!DOCTYPE html> <html> <head> </head> <body> <?php echo "The cookie visitor deleted"; ?> </body> </html>
In the above example, we delete a cookie of name visitor.
To know the Cookie is Enabled or Not in the User Computer
If you want to get information about the cookies are enabled. The below code is used to know the information about the cookies are enabled or not.
Example – 4 Check Cookie is Enable or Not Code
<?php setcookie("visitor", "", time() + 3600); ?> <!DOCTYPE html> <html> <head> </head> <body> <?php if(count($_COOKIE) > 0) { echo "Cookies are enabled."; } else { echo "Cookies are disabled."; } ?> </body> </html>