Cookies

Cookies

It is a method of storing small amounts of data on a computer viewer page (eg. The information about the first and last name or user name on this service). Cookies set by the service are available only for him and are set for some time.
Cookies are transmitted via HTTP headers.

They must be sent to the browser before it is sent to any other content. Therefore, before the Cookie can not connect calls echo and derivatives, as well as the opening tag PHP mode to be the first file znakiami – there can be no blank lines or spaces. This limitation can be overcome by using output buffering.
PHP cookies automatically reads and converts them into variables. They are stored in the superglobal associative array $ _COOKIE (formerly $ HTTP_COOKIE_VARS), in which the keys are the names of cookies. You can set cookies using the setcookie (name, value, czas_wygaśnięcia, path, domain, security). Only the first parameter is required. It means the name of the cookie – this name will be created by PHP variable after rereading cakes. The parameter „value” is probably obvious – the value will be stored in a cookie with the given name. Czas_wygaśnięcia parameter is the time, after which the cookie will be deleted. This time should be given as the number of seconds since 1/1/1970 – this is the time stored in UNIX systems. Current time in this format is returned by the function time (). If the cookie is to be held for an hour, then the time returned by the time () to add the number of seconds contained in the hour – „time () + 3600”. Similarly proceed for other time periods

Intervals for setcookie
• Time – time () + 3600
• Day – time () + 86400
• Month – time () + 30 * 86400
If the time is earlier than the current time, the cookie will be deleted. If it is zero – cookie will be valid only until you close your browser.
The next two parameters are used to indicate the address for which will be available cookiesy. If the last parameter is set to 1, the cookie will be sent via an encrypted HTTPS connection.
Normally there is no possibility of storage arrays in cookies, but you can do it „on about.” Before saving an array of cookie use the function serialize (). Converts an array to a string. To re-read the table is the unserialize ().

Example

<?php
// record
$array = array (‚a’ => ‚first’, ‚b’ => ‚other’);
setcookie (‚wall’, serialize ($array), time () + 3600);

// Read protected nonexistent cookie
if (isset ($ _ COOKIE [ ‚table’])) {
$array = unserialize ($ _ COOKIE [ ‚table’]);
} Else {
$array = array ();
}

?>
In PHP 4.1.0 or newer than there are superglobal $ _REQUEST array, which combines arrays $_GET, $_POST, $_COOKIE, and $_FILES.

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close