Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

PRACTICAL-5

OBJECTIVE- To store a cookie using PHP on client side. ( Set, Retrieve, Delete
cookies)

Set Cookie:

<?php
// Set a cookie named "user" with value "John Doe" that expires in one hour
setcookie("user", "John Doe", time() + 3600, "/");
?>
X---------------------------------------------------
X-------------------------------------------------X

Retrieve Cookie:

<?php
// Check if the "user" cookie is set
if (isset($_COOKIE["user"])) {
$username = $_COOKIE["user"];
echo "Welcome back, $username!";
} else {
echo "Welcome, new visitor!";
}
?>
X-------------------------------------------------------
X------------------------------------------------X

Delete Cookie:

<?php
// Set the expiration time of the "user" cookie to the past
setcookie("user", "", time() - 3600, "/");
?>
X------------------------------------------------------
X------------------------------------------------X

You might also like