CancelImage Upload

How to set and read a cookie in PHP

Cookies are a useful tool for returning visitors. For instance login details and other items can be stored, and old session states from the last visits can be stored based on these.

Defining a cookie in PHP is simple. Here is the function and the corresponding data that can be set:

setcookie(name, value, dateExpire, path, domain, security);

Note that the browser will only send information back to the domain if it does indeed correspond with path. Added to which a secure connection can be established via placing a 1 for security. Here is an example of a cookie definition that will last for an hour (providing the user does not delete it in the meantime):

<?php

setcookie('cookie', 'Hello World', time() + 3600);

?>

And here is how the cookie can be called upon after a subsequent page reload. Note that this variable will not be printed upon the original load, as the cookie setting takes place browser side:

<?php

echo $_COOKIE['cookie'];

?>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com