Tutorial PHP Cookies
Tutorial PHP Cookies
PHP Cookies
A cookie is frequently used to recognize a client. A cookie is a little document that the server implants on the client's PC. Each time a similar PC asks for a page with a program, it will send the cookie as well. With PHP, you can both make and recover cookie esteems.
A cookie is made with the setcookie() function. Just the name parameter is required. Every other parameter are discretionary. The setcookie() function must appear BEFORE the tag. The estimation of the cookie is naturally URLencoded when sending the cookie, and consequently decoded when gotten (to avert URLencoding, utilize setrawcookie).
setcookie(name, value, expire, path, domain, secure, httponly);
PHP Create/Retrieve a Cookie
The accompanying illustration makes a cookie named "client" with the esteem "Jugal Patel". The cookie will lapse following 30 days (86400 * 30). The "/" implies that the cookie is accessible in whole site (generally, select the index you incline toward).
We at that point recover the estimation of the cookie "client" (utilizing the worldwide variable $_COOKIE). We likewise utilize the isset() capacity to see whether the cookie is set.
< ?php
$cookie_name = "user";
$cookie_value = "Jugal Patel";
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
PHP cookie value modify
To alter a cookie, simply set (once more) the cookie utilizing the setcookie() function.
< ?php
$cookie_name = "user";
$cookie_value = "Jugal Patel";
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
PHP cookie delete
To remove a cookie, utilize the setcookie() work with a lapse date previously.
< ?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
? >
< html>
< body>
< ?php
echo "Cookie 'user' is deleted.";
? >
< /body>
< /html>
Output
PHP cookie check if enabled
The accompanying case makes a little script that checks whether cookies are empowered. To begin with, endeavor to make a test cookie with the setcookie() work, at that point tally the $_COOKIE array variable.
< ?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
Posted on Utopian.io - Rewarding Open Source Contributors
Wow so this is how this works
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
thanks alot!
Sorry, but I had to do this wisecrack.
LMFAO
just great!
Hey @brothermic I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x