1. 程式人生 > >php session實現原理分析

php session實現原理分析

keep enc accep referer zip image time -s accept

http://www.jb51.net/article/77726.htm

第一次會話時會有Set-Cookie響應頭返回,設置上PHPSESSID cookie

  1. Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  2. Connection: Keep-Alive
  3. Content-Length: 4865
  4. Content-Type: text/html; charset=utf-8
  5. Date: Mon, 09 Apr 2018 03:24:55 GMT
  6. Expires: Thu, 19 Nov 1981 08:52:00 GMT
  7. Keep-Alive: timeout=5, max=99
  8. Pragma: no-cache
  9. Server: ***********************************************
  10. Set-Cookie: PHPSESSID=710f5dca0eb5ded010b840603c469f2d; path=/
  11. X-Powered-By: **********

之後的請求都會帶上這個cookie作為標識,從而php能夠從服務端的seesion管理中根據seesionid找到該回話的信息

  1. Accept: image/webp,image/apng,image/*,*/*;q=0.8
  2. Accept-Encoding: gzip, deflate
  3. Accept-Language: zh-CN,zh;q=0.9
  4. Connection: keep-alive
  5. Cookie: PHPSESSID=710f5dca0eb5ded010b840603c469f2d
  6. Host: production.whport.com.cn:30000
  7. Referer: http://production.whport.com.cn:30000/SON_EXAM/user/login.php
  8. User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

關了瀏覽器,PHPSESSID cookie 也沒設置生存期,自然這個會話就沒了。

如果設置了 PHPSESSID cookie 生存期,那麽下次打開瀏覽器還會帶著上次的PHPSESSID cookie,自然可以實現保持登錄。

當然php服務端要設置session有效期

php session實現原理分析