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