top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to set and retrieve cookies in PHP?

0 votes
354 views
How to set and retrieve cookies in PHP?
posted Aug 6, 2018 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Hello,

A cookie is a small file that the server embeds on the user's computer.
A cookie is created with the setcookie() function.

Syntax:

setcookie(name, value, expire, path, domain, secure, httponly);

Example:

<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

<?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];
}
?>
answer Jul 12, 2019 by Siddhi Patel
...