Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,449 members, 7,816,042 topics. Date: Friday, 03 May 2024 at 12:39 AM

PHP Cookies For State Management - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Cookies For State Management (820 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)

Software Testers Interview Questions With Answers-part1 / Post Your Wordpress Problems Here / Again... I Will HOST Your Site For FREE... Call Me NOW

(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. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.