keywords: The role of Cookies
1. Record certain information about the visitor. For example, you can use cookies to record the number of times users visit your website,
or the information that visitors have entered, some websites (such as the NetEase community) can automatically record your last login
user name, using a cookie.
2. Pass a variable between pages. The browser does not save any variable information on the current page when
The page is closed and any variable information on the page disappears. If you have a variable on a page
A = 5, to pass this variable to another page, you can use the Http://url?a=5 method to pass the change
or insert a form into the page and insert an implicit field into the form (input hidden
field), passed to the next page in Post/get mode. Another way is to use cookies to change
The amount is saved as a cookie, and then the next page reads the cookie to get the value of the variable.
Note: The cookie must be sent before the other header, otherwise it will go wrong!
The following is an example of a visitor's number of visits:
<?
$HTTP _cookie_vars["Visittimes"]? ($VisitTimes + +):($VisitTimes = 1);
Setcookie ("Visittimes", $VisitTimes, Time () +31536000);
echo "<b> welcome you <font color=\" #FF0000 \ ">". $VisitTimes.
"</font> Visit my homepage </b><br>\n";
?>
The results of the program run are as shown at the top of this page (refresh to see if the number of visits changes).
PHP's Cookie function is:
int Setcookie (string name, string value, int expire, string path,
string domain, int secure);
String Name
the name of the cookie
String Value
the value of the cookie
int Exprie
the expiration date of the cookie, the standard Unix timestamp
string Path,domain
path and domain name of the cookie
int Serure
whether the cookie is transmitted in a secure HTTP manner
where, except for the argument string name is required, the other parameters are optional. int expire is
January 1, 1970 00:00 to a time difference between the seconds. Its default value is 0, which means that the browse
The cookie will be deleted automatically when the device is closed. The above example is valid for one year (365*24*3600=31536000).
again: The cookie must be sent before the other header of the webpage, otherwise error!!
after a Cookie is set, there are two ways to read its value:
direct the name of the cookie as the variable name, i.e. $name.
use $http_cookie_vars["name"].