Principles and relationships between session and cookie in PHP 1. Anyone who has experience in shopping knows that when purchasing a product, the website will record the selected product to the shopping cart, which is achieved by session and cookie technology, of course, if you have experience shopping on different web platform technologies, you will know that when you purchase a product, the website will record the selected product to the shopping cart. these are implemented using session and cookie Technologies, of course, the technical implementation details of different web platforms are slightly different, but session and cookie are almost used.
Why do we need to use session and cookie? it is necessary to talk about the http protocol. the HTTP protocol is stateless. In other words, this second does not know what happened in the previous second,
If you need to track the status of a user on the site, this is obviously not feasible. next we will talk about COOKIE technology.
Cookie: it can be used to share some information on multiple pages. cookies are supported by HTTP. when a browser requests a page from the server, the server returns HTTP packets + data, the browser will parse the HTTP message received from the server for corresponding operations.
Server Message:
HTTP/1.1 200 OK
Date: Thu, 06 Dec 2012 17:05:01 GMT
Server: Apache/2.2.17 (Win32) PHP/5.3.5
X-Powered-By: PHP/5.3.5
Set-Cookie: mycookies = Hello % 2 CCookie; expires = Thu, 06-Dec-2012 18:05:01 GMT; path =/
Content-Length: 44
Keep-Alive: timeout = 5, max = 100
Connection: Keep-Alive
Content-Type: text/html
Here we only focus on Set-Cookie: mycookie = Hello % 2 CCookie; expires = Thu, 06-Dec-2012 18:05:01 GMT; path =/
This is the cookie information that the browser wants to save. In other words, it is: "The browser should save this cookie information"
The format is a key-value pair. for example, if a cookie key is publicinfo, the value is hello, and the cookie format is publicinfo = hello.
Browser request message:
GET/cookie/cookie1.php HTTP/1.1
Accept :*/*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729; Media Center PC 6.0 ;. NET4.0C ;. NET4.0E; Tablet PC 2.0; InfoPath.2)
Accept-Encoding: gzip, deflate
Host: localhost
Connection: Keep-Alive
Cookie: C4vN_2132_saltkey = MW4qwm12; C4vN_2132_lastvisit = 1354808405; C4vN_2132_sid = Dl9pU9; C4vN_2132_lastact = 1354812024% 09search. php % 09 forum; C4vN_2132_forum_lastvisit = D_36_1354812013; C4vN_2132_visitedfid = 36; PHPSESSID = Hangzhou; zhangqiang1 = Hello % 2 CCookie
Here, the browser sends the cookie information of the client to the server, so that some status information can be maintained between the request and response.
Use of cookies in PHP
There are two methods to use cookies in PHP,
1. use the system function setcookie to set
2. use the header function to construct cookie packets
With the previous knowledge, we can know that the two functions actually do the same result. In the end, we need to write the cookie information to the HTTP message.
Method 1:
Setcookie (cookie key name, value, Expiration Time, Access Directory)
Setcookie ("mycookie", "Hello, Cookie", time () + 3600 ,"/");
Echo "viewing cookies ";
?>
Method 2: (the cookie to be set is directly written to the HTTP message)
Header ("Set-Cookie: mycookie = 123; expires = Tue, 04-Dec-2012 15:58:18 GMT ");
Echo "viewing cookies ";
Access the cookie on the server:
Echo $ _ COOKIE ["mycookie"];
?>
You can use the ultra-Global Array $ _ cookie to access the COOKIE. enter the cookie key in.
The principle of using cookies is to know how to implement some shopping websites or login functions, but cookies are also insufficient.
Security: cookies are stored on the client, which can be tampered with and cannot store important data in cookies, such as user names and passwords.
Network Transmission: cookies must be contained in HTTP request and response packets, so they cannot store too much data.
And cookie technology implementation, of course, different web platform technologies...