The role of Cookies
Last Update:2017-02-28
Source: Internet
Author: User
Cookie 1. Record some information about a visitor. For example, you can use cookies to record the number of times users visit your Web page,
or visitors have entered the information, some sites (such as NetEase community) can automatically record your last login
username, using a cookie.
2. Pass variables between pages. The browser does not save any variable information on the current page when the
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 variable
, or insert a form into a Web page, and insert an implied field in 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 value of the variable is obtained by reading the cookie on the next page.
Note: Cookies must be sent before the other header, otherwise error!
Below is an example of the number of visitors visiting a visitor:
;?
$HTTP _cookie_vars["Visittimes"]? ($VisitTimes + +):($VisitTimes = 1);
Setcookie ("Visittimes", $VisitTimes, Time () +31536000);
echo "<b> welcome your <font color=\" #FF0000 > ". $VisitTimes.
"</font> Visit my homepage </b><br>\n";
?>
The results of the
program run as shown at the top of this page (refresh to see if the number of visits varies).
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
cookies are valid for the standard Unix timestamp
string Path,domain
cookie path and domain name
int Serure
whether the cookie is delivered in a secure HTTP manner
which, except that the parameter string name is required, the other parameters are optional. int expire is
the value of the second difference between January 1, 1970 00:00 to a time. Its default value is 0, which means browsing the
The
is turned off and the cookie is automatically deleted. The example above is valid for one year (365*24*3600=31536000).
again: Cookies must be sent before the other header of the page, otherwise error!!
After the
Cookie is set, there are two ways to read its value:
directly to the name of the cookie as the variable name, that is, $name.
uses $http_cookie_vars["name".