The role of Cookies

Source: Internet
Author: User
Tags header insert setcookie variable valid domain domain name
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".








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.