Home > Backend Development > PHP Tutorial > The role of cookies_PHP tutorial

The role of cookies_PHP tutorial

WBOY
Release: 2016-07-13 17:22:28
Original
1008 people have browsed it

1. Record certain information about visitors. For example, you can use cookies to record the number of times users visit your webpage,
or the information that visitors have entered. Some websites (such as NetEase Community) can automatically record the user name you used to log in
last time, using cookies.
 2. Pass variables between pages. The browser will not save any variable information on the current page. When the
page is closed, any variable information on the page will disappear. If you have a variable
a = 5 on a page, and you want to pass this variable to another page, you can use http://url?a=5 to pass the variable
, or insert a form, and insert a hidden field (input hidden
field) into the form and pass it to the next page in POST/GET mode. Another way is to use Cookie, save the variable
in the form of Cookie, and then obtain the value of the variable by reading the Cookie on the next page.
Note: Cookies must be sent before other Headers, otherwise an error will occur!
The following is an example of recording the number of visits by a visitor:

$HTTP_COOKIE_VARS["VisitTimes"]?($VisitTimes ++):($VisitTimes = 1);
setcookie("VisitTimes ",$VisitTimes,time()+31536000);
echo "Welcome ".$VisitTimes.
"
Visit my homepage

";
?>
The result of running the program is shown at the top of this page (refresh to see if the number of visits has changed).
PHP's Cookie function is:
int setcookie(string name , string value, int expire, string path,
string domain, int secure);
string name
cookie name


string value
cookie value
int exprie
Cookie validity period, standard Unix timestamp
string path,domain
Cookie path and domain name
int serure
Whether the cookie is transmitted in a secure http method
Among them, except the parameter string name is required, other parameters are optional. int expire is the second difference between
00:00 on January 1, 1970 and a certain time. Its default value is 0, which means browsing.
If the browser turns off the cookie, it will be automatically deleted. In the above example, the validity period is one year (365*24*3600=31536000).
Once again: the cookie must be sent before other headers on the web page, otherwise an error will occur! !
After the cookie is set, there are two ways to read its value:
Directly use the name of the cookie as the variable name, which is $name.
Use $HTTP_COOKIE_VARS["name"].

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532321.htmlTechArticle1. Record certain information of visitors. For example, you can use cookies to record the number of times users visit your webpage, or the information that visitors have entered. Some websites (such as NetEase Community) can automatically record...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template