Home >Backend Development >PHP Tutorial >PHP creates, obtains cookies and analyzes basic points, phpcookie_PHP tutorial

PHP creates, obtains cookies and analyzes basic points, phpcookie_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:58779browse

php creates, obtains cookies and analyzes basic points, phpcookie

This article analyzes PHP creation, obtaining cookies and basic points with examples. Share it with everyone for your reference. The details are as follows:

Assume: cookie1.php file

Copy code The code is as follows:
setCookie("name","Baidu",time()+60);
echo "Save cookie";
?>

There are three required parameters:

(1) The first parameter: name is the key value, set it yourself;
(2) Second parameter: "Baidu" in the example represents the value corresponding to the key value name;
(3) The third parameter: indicates the expiration time, time()+60, which indicates the expiration time is 60 seconds;

Cookie code analysis and basic points in the example

1. When the browser opens cookie1.php, the server will send the message: Set-Cookie:name=%B0%D9%B6%C8; expires=Tue, 06-Nov-2012 16:09:27 GMT (Remarks : Please use the packet capture tool to view this information) to respond to the http request. The client browser obtains this information and saves it in the cookie file (different browsers and operating systems have different storage locations and file types)
2. If the third time parameter is not set, the cookie will expire when the session ends (close the browser) by default (in this case, the cookie is stored in the browser cache).
3. Cookies can only save string information, that is, objects cannot be saved (session can save objects).
4. If the key value is Chinese, the urlencode method is used by default to transcode the Chinese.
5. When the cookie is saved, it is saved in clear text, so the password needs to be processed, such as md5.
6. Multiple cookies can be saved.
7. Different cookies on the same page can be saved for different times.
8. A website corresponds to a file that saves cookies (if cookies are set).

Get cookies

File: cookie2.php

Copy code The code is as follows:
echo "
";
print_r($_COOKIE);
echo $_COOKIE['name'];
?>

$_COOKIE is a predefined variable (array). During the validity period of the cookie, the above code can be used to output the cookie.
Knowledge points: When the browser accesses the cookie2.php page, it will transmit the cookie information to the server. This is stipulated by the http protocol (you can use the packet capture tool to view it, as shown below)

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947919.htmlTechArticlephp creates, obtains cookies and basic key points analysis, phpcookie This article explains the analysis of php creation, obtaining cookies and basic key points. . Share it with everyone for your reference. The details are as follows: Assume that...
Statement:
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