How To Create Or Retrive Cookie In PHP Example

PHP 8 , PHP

Nicesnippets

351

28-07-2021


How To Create Or Retrive Cookie In PHP Example

Hi guys,

Today i will explained How To Create Or Retrive Cookie In PHP. This example is so easy to use in php.

This Example to use store the user cookie data and store the 1day duration in php.

So let's start to the example.

index.php


<!DOCTYPE html>

<?php

$cookie_name = "user";

$cookie_value = "user name";

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];

}

?>

<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>

</body>

</html>

Output

Cookie named 'user' is not set!

Note: You might have to reload the page to see the value of the cookie.

Now you can check your own.

I hope it can help you...


Recommended Posts