Setting up cookies through PHP
<?So we created a variable named Name, with a value of $Vladimir.
Now, wherever we make a request for the Name variable, Vladimir will be displayed.
Displaying the value of the cookie
//Вывод одного значения
//вывод всех значений
Delete cookieTo delete a cookie value is very simple - to write again with a zero value.
setcookie ("name", '');Or set the time of life, after which the variable itself retires.
setcookie (name, value, expiration time, path, domain);name is the name of the variable;
value - the value of this variable;
expire - the time in seconds since the beginning of the epoch, after which the current cookie becomes unreadable;
path - the path by which the cookie is available;
domain - the domain from which the cookie is available;
secure - If this marker is present, the cookie information is sent only via HTTPS (HTTP using Secure Socket Level SSL), in protected mode. If this marker is not specified, then the information is sent in the usual way.
expire - Life timeThe lifetime is an optional parameter, but very important, because if you do not specify it, the variable will survive until the browser is closed. Cookies will be deleted after an hour (3600 seconds)
setcookie ("Name", "Vladimir", time () +3600);Cookies will be deleted after a year (60 seconds * 60 minutes * 24 hours * 365 days)
setcookie ("Name", "Vladimir", time () + 60 * 60 * 24 * 365);Cookies will be deleted after a year at midnight on January 25, 2014 year
setcookie ("Name", "Vladimir", mktime (0,0,0,01,25,2010));
<?The cook "Name" with the value "Vladimir" will extend 1 hour, it can be read only by the sites located in the directory "/dir/" on the domain "www.ph4.org".
Setcookie ("Name", "Vladimir", time () + 3600, "/dir/", "www.ph4.org");
Check if cookies are enabledFor example, you have created a website that is configured to work with a cookie, and the user has been disabled.
You can do this:
- a visitor hits a page that specifies cookie values,
- Then a query is made for this value,
- If there is no variable, then there is a redirect to a page with text like "you got here because you have cookies disabled." Next is a description of how to enable them and a warning - otherwise some of the functions on this site will be unavailable.
Example of visit counter
echo "You visited the page " . $_COOKIE["counter"] . " times";