In this article, you will learn How to Create Cookies in PHP.on Browser A Cookie is used to identify any user. With the help of the PHP, you can retrieve and create the cookie.

Syntax:-

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

Create or Retrieve Cookies in PHP

Example:-

<?php
$cookie_name = "user";
$cookie_value = "Nisha";
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> Refresh the page to see the value of the cookie.</p>
</body>
</html>

Example:-



<?php
$cookie_name = "user";
$cookie_value = "Neharika";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<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> Refresh the page to see the value of the cookie.</p>
</body>
</html>

You May Also Like:-Condition Statements in PHP