Cookies for PHP development

Source: Internet
Author: User
Tags date header setcookie string variable domain domain name
cookie是一种在浏览器远端存储数据并以此来跟踪和识别用户的机制。简单的说,cookie是web服务器暂时存储在用户硬盘上的一个文件夹,并随时被web浏览器读取。当用户再次访问web网站的时候,网站通过获取cookie记录用户的特定访问信息(如:上次访问的位置,花费的时间,用户名和密码) ,从而迅速做出相应,比如不需要用户输入密码就可以登录。
文本文件的格式如下:用户名@网站地址[数字].txtcookie 的功能主要有以下几个方面:

1, record some information of visitors. You can use cookies to record the number of times a user accesses a page, or the information that a user has entered. In addition, some Web sites can automatically record the last user name that can be logged in.
2, passing variables between pages. The browser does not save any variable information for the current page, and when the page is closed, all the variable information on the page disappears. If you declare a variable id=8, to pass this variable to another page, you can save the variable as a cookie and then read the value of the variable in the cookie on the next page.
3, the Internet page that you want to view is stored in the cookie Temp folder, which can improve the access speed of the browser later.
Create cookies in PHP using the Setcookie () function. A cookie is part of an HTTP header and the header must be sent before the rest of the page, and he must first output. outputting an HTML tag before the Setcookie () function, or an echo statement, or even a blank line can cause a program error.
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 described as follows:


Use the Setcookie () function to create the cookie sample code as follows:


  
   

Read the cookie
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 a cookie's variable. The sample code is as follows:


  
   

The results of the operation are as follows:

Delete Cookies
The deletion of cookies is mainly achieved by using the Setcookie function, which is to reduce the expiration time of the cookie by 1 seconds, as follows:
Setcookie ("" Visittime), Time ()-1);

The life cycle of cookies
If the cookie does not set a time, it means that its lifecycle is the duration of the browser session, and the cookie is deleted as long as the browser is closed. This cookie is called a session cookie and is generally not stored on the hard disk, but in memory.
If the cookie sets the expiration time, the browser saves the cookie to the hard disk, and the cookie still laughs when the browser is opened again until the cookie expires.
The maximum number of cookies that the browser allows to store is 300, and each cookie file supports a maximum capacity of 4k, with a maximum of 20 cookies per domain name. If the limit is reached, the browser will randomly delete cookies.



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.