PHP cookies
A cookie is used for identification purposes. It is more commonly used to identify a user in a session. It is a small file the application inserts on the users computer. With PHP one can create and retrieve the cookie.
Setting cookie in php:
Cookies in PHP can be set using the setcookie() function. This must appear before the HTML tag.
Syntax:
Setcookie(name, value, expire, path, domain);
Example: here, the cookie name sample is assigned a value jim. The cookie expires after an hour.
Setcookie(“sample”, “jim”, time()+3600);
Retrieving cookie value:
The cookie that is set can be retrieved as shown below:
Echo $_cookie[“user”];
Isset() function can be used to find if the cookie is set.
What is a Persistent Cookie?
Cookies are used to remember the users. Content of a Persistent cookie remains unchanged even when the browser is closed. ‘Remember me’ generally used for login is the best example for Persistent Cookie.
How to set cookies? How to reset/destroy a cookie?
Setting cookie in php:
Cookies in PHP can be set using the setcookie() function. This must appear before the HTML tag,
Syntax:
Setcookie(name, value, expire, path, domain);
Example: here, the cookie name sample is assigned a value jim. The cookie expires after an hour.
Setcookie(“sample”, “jim”, time()+3600);
Reset/destroy cookie
Cookies can be deleted either by the client or by the server. Clients can easily delete the cookies by locating the Cookies folder on their system and deleting them. The Server can delete the cookies in two ways:
- Reset a cookie by specifying expiry time
- Reset a cookie by specifying its name only