PHP Developer's Cookie

Source: Internet
Author: User
Tags setcookie

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

1. Record certain information about the visitor. If you can use cookies to record the number of times a user visits a page, or the information that the user has entered. In addition, some websites 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 variable information on the page disappears. If you declare a variable id=8, to pass the variable to another page, you can first save the variable as a cookie and then read the value of the variable in the cookie on the next page.
3, the requested view of the Internet page is stored in the cookie temporary folder, you can improve the speed of future browser access.
Create a cookie in PHP with the Setcookie () function. The cookie is part of the HTTP header and the header must be sent before the rest of the page, and he must first output it. outputting an HTML tag before the Setcookie () function, or an echo statement, or even a blank line, can cause the program to fail.
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 below:


The sample code for creating cookies using the Setcookie () function is as follows:

<?php//setcookie ( "TMCookie" ,  ' www.baidu.com ' ); //setcookie  ( "Tmcookie" ,  ' www.baidu.com ' , time      () +60 ); //cookie  is valid for 60  sec//Set effective time to 60  seconds, valid directory is  "/temp/" , valid for domain name  "www.baidu.com"  and all its subdomains  $value  =88 ; Setcookie (" Tmcookie ", $ Value , time  () +3600 ,  "/temp/" ,  ". baidu.com" );?  

reading of Cookies
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 variables of the cookie. The sample code is as follows:

<?phpif(!isset($_cookie["Visittime"])){//Detect if cookie file exists, if not presentSetcookie ("Visittime", Date ("y-m-d h:i:s"));//Set a cookie variable    Echo "Welcome to visit the website for the first time!!! ";//Output string}Else{Setcookie ("Visittime", Date ("y-m-d h:i:s"), time () + -);Echo "Last Access time:".$_cookie["Visittime"]."\ n";}Echo "The time of this visit is:". Date ("y-m-d h:i:s")."\ n";?>

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, title as follows:
Setcookie ("" Visittime), Time ()-1);

the life cycle of a cookie
If the cookie does not set a time, it indicates that its lifetime is the period of the browser session, and the cookie is deleted if the browser is closed. This cookie, known as a session cookie, is generally not saved on the hard disk, but in memory.
If the cookie is set to expire, then the browser will save the cookie to the hard drive and the cookie still laughs when the browser is opened again until the cookie expires.
Browsers allow up to 300 cookie files to be stored, and each cookie file has a maximum support capacity of 4k, with a maximum of 20 cookies per domain name. If the limit is reached, the browser will randomly delete the cookies.

PHP Developer's Cookie

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.