Cookie function

Source: Internet
Author: User
Tags setcookie
1. Record some information of the visitor. For example, you can use cookies to record the number of times a user visits your webpage,
Or some websites (such as the NetEase community) can automatically record the information you have logged on.
Is the Cookie.
2. Pass variables between pages. The browser does not save any variable information on the current page.
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 http: // url? A = 5 transmission changes
Or insert a form in the webpage and insert an implicit field (input hidden
Field), passed to the next page in POST/GET mode. Another way is to use cookies to change
Volume is saved as a Cookie, and then the variable value is obtained by reading the Cookie on the next page.
Note: The Cookie must be sent before other headers; otherwise, an error occurs!
The following example records the number of visits of a visitor:
$ HTTP_COOKIE_VARS ["VisitTimes"]? ($ VisitTimes ++) :( $ VisitTimes = 1 );
Setcookie ("VisitTimes", $ VisitTimes, time () + 31536000 );
Echo "<B> Welcome <font color =" # FF0000 ">". $ VisitTimes.
"</Font> Visit my homepage </B> <br> ";
The running result 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 secure http mode
Among them, except the string name parameter is required, other parameters are optional. Int expire is
The second difference between on January 1, January 1, 1970 and a certain time. Its default value is 0, that is, browsing
When the cookie is disabled, it is automatically deleted. In the preceding example, the validity period is one year (365*24*3600 = 31536000 ).
Again, the cookie must be sent before other headers on the webpage. Otherwise, an error occurs !!
There are two methods to read the Cookie value after it is set:
Directly use the cookie name as the variable name, that is, $ name.
Use $ HTTP_COOKIE_VARS ["name"].

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.