PHP Programming/Cookies
From Wikibooks, the open-content textbooks collection
[edit] Cookies
Cookies are small text files stored on the client's computer which can contain various types of data including user preferences, time and more. Cookies were first introduced by Netscape. PHP allows easy setting and retrieving of cookies.
[edit] Set a cookie
The PHP setcookie() function has the form
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]])
where name is the cookie name, value is the data to be contained in the cookie, expire the time after which the cookie should expire, path the path on the server which can use the cookie, domain can be used to set permissions for subdomains and secure if set true only transmits the cookie if a secure connection is present.
Since all cookies are sent by the server alongwith HTTP headers you need to set any cookie at the start of a page before any other code. You will normally only need to use the name, value and expire arguments. If expire not set the cookie will expire when the client closes the browser.
|
<?php setcookie("wikibooks", "user", time()+3600); ?> |
The above code will set a cookie having the name wikibooks, value user and will expire an hour after it is set.