Php advanced: flexible use of cookies

Source: Internet
Author: User

Cookie is a powerful and convenient function. It provides a full range of variables to see the cookie Syntax:
Setcookie (cookievalue, value, time, path, domain );
Cookievalue is the cookie variable, value is the cookie variable, time is the time when the cookie variable takes effect, and path is the path where the cookie acts, domain is the domain used by the cookie;
It should be noted that when setting cookies in php programs, they must not be set after the html Tag.
The following is an example:
Setcookie ("cookietime", "2001-5-2", time () + 3600, "/", "test. php ");
This cookie is used to set a cookie variable $ cookietime for the date "2001-5-2", which takes effect in/test. php In 3600 seconds.
Then echo $ cookietime; added to test. php will display--2.
If the time in the cookie is set to null, the cookie becomes invalid after the browser is closed.
The cookie value is not executed immediately until it is referenced for the second time. What is the cookie value referenced for the first time?
It is the value set for the last cookie. It is difficult to understand it here. For example, it is clear:
Setcookie ("cookietime", time, time () + 3600, "/", "test. php ");
No value in $ cookietime for the first reference. The time in $ cookietime is displayed only when the second reference is made. The time is 2001:
Setcookie ("cookietime", 2001, time () + 3600, "/", "test. php ");
Once again, $ cookietime is referenced for the first time and $ cookietime is referenced for the second time only 2001. What is the function of this feature of cookie? Smart readers may already know how to use it, that is, to use the notification and alarm functions;
Let's take a look at the example of using the cookie feature. The function that the program implements here is that when the user visits the website next time, all the new information that this user has not read is followed by a new (new). It is no longer easy to use cookies to complete this function, at least in my opinion, there is no other way to do this function more easily than using cookies.
Storing information naturally requires a time value. As long as the database has a time value related to each piece of information and a cookie, this looks good:
Test. php:
$ Time = date ('Y-m-d H: I: s ');
Setcookie ("cookietime", $ time, time () + 3600000, "/", "test. php ");
......
......
// Extract the time value from the database

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.