Tutorial PHP Cookies

in #utopian-io7 years ago

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
1.png

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
2.png

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

3.png

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
4.png



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

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

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.20
TRX 0.16
JST 0.030
BTC 65834.12
ETH 2676.99
USDT 1.00
SBD 2.89