• PHP SESSION的工作原理解析(转)


    众所周知,http协议是一个无状态协议,简单来说就是,web服务器是不知道现在连接上来的人到底是哪个人,为了满足选择性发送信息的需求,在http的基础上做了很多扩展来达到这个目的,如数字签名、cookie、session等。
    web服务器或者web程序如何能够知道现在连接上来的是谁?要解决这个问题,首先需要在服务器端和客户端建立一一对应关系,下边我通过抓取http的内容来说明这种对应关系是如何建立的。
    我使用的是一个叫做httplook的http包嗅探工具,然后在本地web服务器的根目录下建立一个叫test.php的文件,地址是:http://localhost/test.php,一切就绪以后我通过浏览器反复打开这个页面。

    以下是前两次向服务器发出的信息及服务器返回的信息

    1. <?php
    2. session_start();
    3. if (isset($_SESSION['test_sess'])){
    4.     $_SESSION['test_sess']++;
    5. }else{
    6.     $_SESSION['test_sess'] = 0;
    7. }
    8. echo $_SESSION['test_sess'];
    9. ?>;
    原帖由 "第一次请求服务器" 发表:

    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
    原帖由 "服务器第一次返回" 发表:

    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
    原帖由 "第二次请求服务器" 发表:

    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
    原帖由 "服务器第二次返回" 发表:

    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附带在请求信息中
      
      转:http://bbs.chinaunix.net/viewthread.php?tid=600493
  • 相关阅读:
    使用公钥和私钥实现LINUX下免密登录
    XML入门
    JSP页面中的errorPage属性和web.xml<error-page>标签的区别
    JAVA、TOMCAT环境变量配置
    在Eclipse Neon中导入serlvet-api等jar包
    56. Merge Intervals
    55. Jump Game
    34. Find First and Last Position of Element in Sorted Array
    33. Search in Rotated Sorted Array
    3. Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/jghdream/p/3406184.html
Copyright © 2020-2023  润新知