PHP Session原理

來源:互聯網
上載者:User
眾所周知,http協議是一個無狀態協議,簡單來說就是,web伺服器是不知道現在串連上來的人到底是哪個人,為了滿足選擇性發送資訊的需求,在http的基礎上做了很多擴充來達到這個目的,如數位簽章、cookie、session等。
web伺服器或者web程式如何能夠知道現在串連上來的是誰?要解決這個問題,首先需要在伺服器端和用戶端建立一一對應關係,下邊我通過抓取http的內容來說明這種對應關係是如何建立的。
我使用的是一個叫做httplook的http包嗅探工具,然後在本地web伺服器的根目錄下建立一個叫test.php的檔案,地址是:http://localhost/test.php,一切就緒以後我通過瀏覽器反覆開啟這個頁面。

;


以下是前兩次向伺服器發出的資訊及伺服器返回的資訊


QUOTE:

原帖由 "第一次請求伺服器" 發表:

GET /test.php HTTP/1.1
Accept: */*
Referer: http://localhost/
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)
Host: localhost
Connection: Keep-Alive




QUOTE:

原帖由 "伺服器第一次返回" 發表:

HTTP/1.1 200 OK
Date: Fri, 26 Aug 2005 07:44:22 GMT
Server: Apache/2.0.54 ( Win32) SVN/1.2.1 PHP/5.0.4 DAV/2
X-Powered-By: PHP/5.0.4
Set-Cookie: PHPSESSID=bmmc3mfc94ncdr15ujitjogma3; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 1
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Content-Language: Off




QUOTE:

原帖由 "第二次請求伺服器" 發表:

GET /test.php HTTP/1.1
Accept: */*
Referer: http://localhost/
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)
Host: localhost
Connection: Keep-Alive
Cookie: PHPSESSID=bmmc3mfc94ncdr15ujitjogma3




QUOTE:

原帖由 "伺服器第二次返回" 發表:

HTTP/1.1 200 OK
Date: Fri, 26 Aug 2005 07:44:23 GMT
Server: Apache/2.0.54 (Win32) SVN/1.2.1 PHP/5.0.4 DAV/2
X-Powered-By: PHP/5.0.4
Set-Cookie: PHPSESSID=bmmc3mfc94ncdr15ujitjogma3; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 1
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Content-Language: Off




仔細對比這些輸出,第二次請求比第一次請求多出來的就是:
Cookie: PHPSESSID=bmmc3mfc94ncdr15ujitjogma3
這個header將會向伺服器發送一個cookie資訊,告訴伺服器我有一個cookie,名字叫PHPSESSID,內容是bmmc3mfc94ncdr15ujitjogma3。
這個cookie是怎麼來的呢?看第一次伺服器返回的資訊裡邊有:
Set-Cookie: PHPSESSID=bmmc3mfc94ncdr15ujitjogma3; path=/
這是伺服器向用戶端瀏覽器寫一個cookie,名字是PHPSESSID,值是bmmc3mfc94ncdr15ujitjogma3,這個值實際就是所謂的session_id。
繼續看第二次向伺服器發出的請求,仍然向伺服器發送了PHPSESSID這個cookie

可以得到以下結論:
1、只要使用了session,就會通過cookie的方式向用戶端瀏覽器發送session
2、每次向伺服器發出請求的時候,本地瀏覽器會把cookie附帶在請求資訊中

說到這裡,伺服器端和用戶端如何通過session做到一一對應的答案就很清楚了,明白了這個道理,對於使用session有很大協助,請細細體會。

轉:http://bbs.chinaunix.net/viewthread.php?tid=600493

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.