How to use cookies
The management of cookies in PHP is done by using the statement setcookie, this statement is available since version 3 of PHP.
int setcookie (string Name [, string Value [, int Expire [, string Path [, string Domain [, int Secure]]]]])
Setcookie() defines a cookie that is sent along with the rest of the information from the header. Cookies shall be sent before any html tag; therefore, we shall call one of these statements before any tag <HTML> or <HEAD>. This is a restriction of cookies, not of PHP.
All messages, except name, are optional.
- Name. Name of the cookie. If we create a cookie only with its name, the cookie existing in the client under said name will be deleted. We can also replace any argument with an empty string("").
- Value. Value to be stored by the cookie in the client.
- Expire. The argument expire is an integer argument that indicates the time a cookie will be deleted in the time format returned by the UNIX statements time() and mktime(). Time() + N seconds of duration is generally used to specify the duration of the cookie.
- Path. Subdirectory where the cookie has a value.
- Domain. Domain where the cookie has a value. If we establish www.domain.com as domain, the cookie is not set for domain.com. Meanwhile, if we establish domain.com as domain, the cookie is set as for domain.com as for www.domain.com.
- Secure. The message secure indicates that the cookie will only be set by a secure HTTPS connection.
Example
setcookie("usuario", "Luis", time()+3600,"/","webestilo.com");
In this example, we set a user name cookie that has the value Luis, lasts 1 hour (3600 seconds) valid for the whole domain webestilo.com