Home > Backend Development > PHP Tutorial > PHP Advanced: Use Cookies Flexibly_PHP Tutorial

PHP Advanced: Use Cookies Flexibly_PHP Tutorial

WBOY
Release: 2016-07-20 11:04:46
Original
816 people have browsed it

Cookie is a powerful and convenient function. It can provide a full range of variables. Let’s take a look at the syntax of cookie:
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, certain It cannot be set after the html tag.
Look at an example below:
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 in test Adding echo $cookietime; in .php will display 2001-5-2.
If you leave the time in the cookie blank, the cookie will become invalid after closing the browser.
There is a special place in cookies That is, the value set by the cookie will not be executed immediately. It 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, it doesn’t matter, just give an example and you will understand:
setcookie("cookietime",time,time()+3600,"/","test.php");
$cookietime when quoted for the first time There is no value in it. When I reference it for the second time, I find that the time in $cookietime is displayed. I modify the time to 2001:
setcookie("cookietime",2001,time()+3600,"/","test.php ");
Execute it again. The value of $cookietime is time when it is referenced for the first time, and the value of $cookietime is 2001 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 is for notification and alarm functions;
Look at an example of using the cookie feature. The function to be implemented by the program here is that when the user browses the website next time, all the things that the user has not seen Add a (new) after new information. It couldn’t be simpler to use cookies to complete this function. At least in my opinion, there is no other method simpler than using cookies to complete this function.
Storing information is natural A time value is required. As long as the database stores the time value related to each piece of information and adds cookies, this seemingly good function can be completed:
test.php:
$time=date('Y-m-d H:i :s');
setcookie("cookietime",$time,time()+3600000,"/","test.php");
......
.... ..
//Get the time value from the database

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445177.htmlTechArticleCookie is a powerful and convenient function. It can provide a full range of variables. Let’s take a look at the syntax of cookie. : setcookie(cookievalue,value,time,path,domain); cookievalue is the set coo...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template