PHP Cookies
PHP Cookies
The rst time when a client sends a request to the server, the
server embedded the cookie along with the response. Next time
onwards client sends the request along with a cookie to the
server. In such a way, cookies can be received on the server side
fi
.
Syntax:
Example2:
setcookie("username","StudyGlance",time()
+1*60*60);
In the above example, de ning a name and value for cookie along
with expiry time (1 hour = 1*60*60 seconds or 3600 seconds
Retrieve a cookie :
Example1
fi
fi
.
Example: "cookiedemo.php"
<?php
// To create cookie
setcookie("username", "NETAJI");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["username"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["username"];
}
?>
</body>
</html
Output
>
In the above example program, Firstly cookie is not set. But, if you
refresh the page, you will see cookie is set now. And it creates the
cookie "username" along with the value "NETAJI" and the default
expiry time 0. i,e the cookie will expire as soon as after closing
the browser window
.