Skip to content

All topics  /  PHP

How To Create Or Retrive Cookie In PHP Example

PHP ·

Teams applying “How To Create Or Retrive Cookie In PHP Example” in a production project can use immigration reform control act irca to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

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

# PHP 8

# PHP