Home  >  Article  >  php教程  >  Use cookies flexibly

Use cookies flexibly

高洛峰
高洛峰Original
2016-12-02 10:28:071203browse

Cookie is a powerful and convenient function. It can provide a full range of variables. Let’s take a look at the cookie syntax:

setcookie(cookievalue, value, time, path, domain);

cookievalue is the set cookie variable. value is a variable that sets a value to a cookie, time is the time when the cookie variable acts, path is the path where the cookie acts, and domain is the domain where the cookie acts;

It should be noted that when setting cookies in the PHP program, you must not set them in html The setting behind this tag.

Let’s look at an example:

setcookie("cookietime","2001-5-2",time()+3600,"/","test.php");

This Cookie setting means to set a cookie variable $cookietime for the date "2001-5-2", its action time is 3600 seconds, this variable takes effect in /test.php.

At this time, add echo $ in test.php cookietime; will display 2001-5-2.

If you leave the time in the cookie blank, the cookie will become invalid after closing the browser.

A special thing about cookies is that the value set by the cookie will not It will be executed immediately and will not be executed until the second reference. So what is the value of the cookie referenced for the first time?
It is the value set last time by the cookie. It is a bit difficult to understand here. It doesn’t matter. I will understand it all with an example. Now:

setcookie("cookietime",time,time()+3600,"/","test.php");

There is no value in $cookietime when it is referenced for the first time, but $cookietime is found when it is referenced for the second time. The time within is displayed, modify the time to 2001:

setcookie("cookietime",2001,time()+3600,"/","test.php");

Execute again, first reference The value of $cookietime is time at that time, and the value of $cookietime is 2001 only when it is referenced for the second time; so what is the function of this feature of cookie? Smart readers may already know its usage, which is used for notification and alarm functions;

Look at an example of using the cookie feature. The function that the program wants to implement is that when the user browses the website next time, all new information that the user has not seen will be added with a (new). Cookies are used to complete this The function couldn't be simpler, at least in my opinion, there is no other method simpler than using cookies to complete this function.

Storing information naturally requires a time value, as long as the database stores the time value related to each piece of information Adding cookies can complete this seemingly good function:

test.php:

$time=date('Y-m-d H:i:s');
setcookie("cookietime",$time,time() +3600000,"/","test.php");

......
......
//Get the time value from the database
$datatime=mysql_result($result,$i," time");

//Compare the size of the two times, all information where $datatime is greater than $cookietime is followed by (new)
if ($datatime>$cookietime)
echo "(new)";
... ...
...


The whole process is so simple and clear. When the user sends a request to the website to browse/test.php, his browser will show him the last time he browsed/test.php The cookie record is sent to the server. The server accepts the cookie value and processes it. At the same time, it resets the cookie of the user's browser and returns the processing result. This is how cookies work.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn