PHP » Cookies and Sessions » setcookie()

boolean setcookie(string name [, string value [, integer expire [, string path [, string domain [, integer secure]]]]])
The name with which the cookie will later be accessed.
Value of the cookie.
Sets a time when the cookie will be deleted from the browser. If it is omitted, the browser will remove the cookie when the session ends (usually when the browser is closed). The time should be specified as an absolute time given as a number of seconds since January 1, 1970. The current time can be fetched with the time() function.
Specifies a web site document path in which the cookie will be sent. Outside this path the cookie is not valid. If left out, it will be set to the path of the document creating the cookie.
The domain that the cookie is valid in. The default value is the server that created the cookie. If the site spans several servers that need to access the cookie, their common domain needs to be chosen. The domain cannot be a top domain and not a domain that the server creating the cookie does not belong to.
Whether the cookie must be retrieved in a secure setting (SSL). Default is FALSE.

Sets a cookie in the browser.

A cookie is a piece of information from the web server that is stored in the user's web browser. One important use for cookies is identifying the user, which allows site personalization and tracking of users among other things.
Cookies must be sent before anything else to the browser. It is also important to remember that a cookie cannot be used in the same document that set it. It will first be available the next time the browser makes a request, i.e. when a new page is loaded or the current page is reloaded.



if (!isset($_COOKIE["identity"])) {
  setcookie("identity", uniqid(""));
  print "No ID yet!";
} else {
  print "My ID is " . $_COOKIE["identity"];

My ID is 401d856082bd6

The first time the page is loaded the cookie hasn't been created yet. When the page is reloaded the ID is printed.