Php Cookies

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Create Cookies

A cookie is created with the setcookie() function.

Syntax :-

Example :-

Result

Cookie named 'user' is not set!

Note: You might have to reload the page to see the value of the cookie.

Modify a Cookie Value

To modify a cookie, just set (again) the cookie using the setcookie() function:

Example :-

Result

Cookie 'user' is set!
Value is: Smith

Note: You might have to reload the page to see the new value of the cookie.

Delete a Cookie

To delete a cookie, use the setcookie() function with an expiration date in the past:

Example :-

Result

Cookie 'user' is deleted.