Saturday, October 7, 2017

PHP Cookies for State Management

skptricks php cookies

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), "/"); // 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>

Modify a Cookie Value

To modify a cookie, just set again the cookie using the setcookie() function and it will overwrite the content of cookie:
Below example here we are using the same code to modify the cookie value
<?php
$cookie_name = "user"; // set the cookie name
$cookie_value = "Amit kumar singh"; // set the cookie value
// set the expire time for cookie.
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 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>

Delete a Cookie

To delete a cookie, use the setcookie() function with an expiration date in the past. lets see the below example :
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
Note :  The setcookie() function must appear BEFORE the <html> tag.

Check if Cookies are Enabled or Not

Let see the below example to create a small script that checks whether cookies are enabled or not.
  1. create the cookie by using setcookie() function.
  2. check the length or count the store value, based on that we verify whether it is enabled or not.

<?php
// creating the cookie to start session
setcookie("key_value", "45544543", time() + 3600, '/');
?>
<html>
<body>

<?php
// checking cookie is exist or not
if(count($_COOKIE) > 0) {
    echo "Cookie are enabled.";
} else {
    echo "Cookie are disabled.";
}
?>

</body>
</html>


No comments:

Post a Comment