Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,985 members, 7,956,679 topics. Date: Monday, 23 September 2024 at 04:50 PM

PHP Cookies For State Management - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Cookies For State Management (836 Views)

Who Can Pentest A Webform For Hidden Cookies And Tokens / Firefox 65 Will Block Tracking Cookies By Default / PHP Session For State Management (2) (3) (4)

(1) (Reply)

PHP Cookies For State Management by skptricks: 8:03am On Oct 19, 2017
Link : http://www.skptricks.com/2017/10/php-cookies-for-state-management.html

What is a Cookie?

A cookie is a small file that the server embeds on the client browser. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
Note : Cookie is created at server side and saved to client browser.

How to Create Cookies With PHP

We are using setcookie() function to create cookies in client browser machine.
setcookie(name, value, expire, path, domain, secure, httponly);

PHP Create/Retrieve a Cookie
Lets see the below simple example to create the cookie and retrieve the value from cookie.
Note : The setcookie() function must appear BEFORE the <html> tag.
<?php
$cookie_name = "user"; // set the cookie name
$cookie_value = "sumit"; // set the cookie value
// set the expire time for cookie.
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"wink; // 86400 = 1 day
?>
<html>
<body>

<?php
// checking cookie is created or not
if(!isset($_COOKIE[$cookie_name])) {
// display message when cookie is not exist
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
// it will print value for cookie
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

(1) (Reply)

Complete Cryptocurrency Marketplace Script Available For Grabs. / Learn To Build Online Stores For FREE - Whatsapp Training - 08029345672 / Legit Itunes Buyer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.