Cookie

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:13:07
Original
1133 people have browsed it

1. What is Cookie?

Cookie is a mechanism that stores data on the client's browser to track and identify users. Simply put, a Cookie is a text file that is temporarily stored on the user's hard drive by the Web server and is subsequently read by the Web browser. When the user visits the website again, the website records the visitor's specific information (such as the location of the last visit, time spent, user name and password, etc.) by reading the cookie file to respond quickly. For example, you can directly log in to the website without entering the user's ID and password on the page.

The command format of the Cookie file is as follows:

Username@website address [number].txt

2. Create a

Cookie

Create a

Cookie through the setcookie() function in PHP.

Use the setcookie() function to create

Cookie. The example code is as follows:

Example 1:

setcookie("TMCookie",'www.mrbccd.com');
setcookie("TMCookie" , 'www.mrbccd.com', time()+60); //Set the cookie validity time to 60 seconds
//Set the validity time to 60 seconds, the valid directory is "/tm/", and the valid domain name is "mrbccd. com" and all its subdomains
setcookie("TMCookie", $value, time()+3600, "/tm/",". mrbccd.com", 1);
?>
3. Read Cookie

In PHP, you can read the cookie value on the browser side directly through the global array $_COOKIE[].

Example 2: Use the print_r function to read Cookie variables. The example code is as follows:

date_default_timezone_set("Etc/GMT-8");
if(!isset($_COOKIE["visittime"])) {// If the cookie does not exist
SetCookie ("Visittime", date ("Y-M-D H: I: S")); // Set a cookie variable
ECHO "Welcome to visit the website for the first time!" & Lt; br & gt ;"; //Output string
}else{              //If Cookie exists
                                                                                                                                                        Variables c Echo "The time for your last visit to the website is:". $_ Cookie ["Visittime"]; // Output the time of the last visit to the website
echo "& lt;"
echo "The time you visited the website this time is: ".date("y-m-d H:i:s"); //Output the current access time
?>

4. Delete Cookie
When the cookie is created , if its expiration time is not set, its cookie file will be automatically deleted when the browser is closed.

If you want to delete the Cookie file before closing the browser, there are two methods: one is to use the setcookie() function to delete, the other is to manually delete the Cookie in the browser.

The above has introduced Cookies, including Web server aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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