© Mikael Olsson 2016

Mikael Olsson, PHP 7 Quick Scripting Reference, 10.1007/978-1-4842-1922-5_24

24. Cookies

Mikael Olsson

(1)Hammarland, Finland

A cookie is a small file kept on the client’s computer that can be used to store data relating to that user.

Creating Cookies

To create a cookie, the setcookie function is used. This function must be called before any output is sent to the browser. It has three mandatory parameters that contain the name, value, and expiration date of the cookie.

setcookie("lastvisit", date("H:i:s"), time() + 60*60);

The value here is set with the date function, which returns a string formatted according to the specified format string. The expiration date is measured in seconds and is usually set relative to the current time ...

Get PHP 7 Quick Scripting Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.