Home >Backend Development >PHP Tutorial >Cookies for php development_PHP tutorial
<code>cookie是一种在浏览器远端存储数据并以此来跟踪和识别用户的机制。简单的说,cookie是web服务器暂时存储在用户硬盘上的一个文件夹,并随时被web浏览器读取。当用户再次访问web网站的时候,网站通过获取cookie记录用户的特定访问信息(如:上次访问的位置,花费的时间,用户名和密码) ,从而迅速做出相应,比如不需要用户输入密码就可以登录。 文本文件的格式如下:用户名@网站地址[数字].txtcookie 的功能主要有以下几个方面: </code>
1. Record certain information about visitors. For example, cookies can be used to record the number of times a user visits a page, or the information the user has entered. In addition, some websites can automatically record the last user name that can be logged in.
2. Pass variables between pages. The browser does not save any variable information of the current page. When the page is closed, all variable information on the page will disappear. If you declare a variable id=8 and want to pass this variable to another page, you can first save the variable in the form of a cookie, and then read the value of the variable in the cookie on the next page.
3. Store the Internet page you want to view in the cookie temporary folder, which can improve the browser's access speed in the future.
Create cookies through the setcookie() function in php. The cookie is part of the HTTP header and the header must be sent before other content on the page. It must be output first. If an HTML tag, echo statement, or even a blank line is output before the setcookie() function, a program error will occur.
The syntax format is as follows:
bool setcookie(string name[,string value[,int explre[,string path[,string domain[,int secure]]]]])
The parameters of the setcookie() function are explained as follows:
Use the setcookie() function to create a cookie. The sample code is as follows:
<code class="hljs perl"><!--?php //setcookie("TMCookie",'www.baidu.com'); //setcookie("TMCookie",'www.baidu.com',time()+60); //cookie的有效期为60秒 //设置有效时间为60秒,有效目录为"/temp/",有效于域名为"www.baidu.com" 及其所有子域名 $value =88; setcookie("TMCookie",$value,time()+3600,"/temp/",".baidu.com"); ?--></code>
Cookie reading
In php, you can directly use the global array $_COOKIE[] to read the value of the browser's cookie.
Use the print_r() function to read the cookie's variables. The sample code is as follows:
<code class="hljs xml"><!--?php if(!isset($_COOKIE["visittime"])){ //检测cookie文件是否存在,如果不存在 setcookie("visittime",date("Y-m-d H:i:s")); //设置一个cookie变量 echo "欢迎第一次访问网站!!!"; //输出字符串 }else{ setcookie("visittime",date("Y-m-d H:i:s"),time()+60); echo "上次访问的时间为:".$_COOKIE["visittime"]."\n"; } echo "本次访问的时间为:".date("Y-m-d H:i:s")."\n"; ?--></code>
The running results are as follows:
Delete cookies
Deleting cookies is mainly achieved by using the setcookie function, which is to reduce the cookie expiration time by 1 second, as shown in the title:
setcookie(“”visittime),time()-1);
Cookie life cycle
If the cookie does not set a time, it means that its life cycle is during the browser session. As long as the browser is closed, the cookie will be deleted. This kind of cookie is called a session cookie and is generally not stored on the hard disk but in memory.
If the cookie is set to expire, the browser will save the cookie to the hard drive, and the cookie will still be there when you open the browser again until the cookie's validity period expires.
The browser allows up to 300 cookie files to be stored, and each cookie file supports a maximum capacity of 4k, and each domain name supports a maximum of 20 cookies. If the limit is reached, the browser will randomly delete cookies.