【转】php session原理,值得品味学习!

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




<?php

session_start();

if (isset($_SESSION['test_sess'])){

     $_SESSION['test_sess']++;

}else{

     $_SESSION['test_sess'] = 0;

}

echo $_SESSION['test_sess'];

?>;



以下是前两次向服务器发出的信息及服务器返回的信息
引用:原帖由 "第一次请求服务器" 发表:

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附带在请求信息中

说到这里,服务器端和客户端如何通过session做到一一对应的答案就很清楚了,明白了这个道理,对于使用session有很大帮助,请细细体会。

深入学习:http://hi.baidu.com/xchinux/blog/item/428540a73c877793d0435838.html


我的httpwatcheer情况:
第一次:
server:
HTTP/1.1 200 OK
Date: Thu, 05 Feb 2009 11:24:42 GMT
Server: Apache/2.2.9 (APMServ) PHP/5.2.6
X-Powered-By: PHP/5.2.6
Set-Cookie: PHPSESSID=b6d43eeccc1c4682ce37be790386e15e; 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: 2
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

0;

client get:

GET /5-5/session.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: localhost
Connection: Keep-Alive

刷新第二次:
server:

HTTP/1.1 200 OK
Date: Thu, 05 Feb 2009 11:26:22 GMT
Server: Apache/2.2.9 (APMServ) PHP/5.2.6
X-Powered-By: PHP/5.2.6
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: 2
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

1;

client get:

GET /5-5/session.php HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: localhost
Connection: Keep-Alive
Cookie: PHPSESSID=b6d43eeccc1c4682ce37be790386e15e

果然发现第一次server中包含:
Set-Cookie: PHPSESSID=b6d43eeccc1c4682ce37be790386e15e; path=/  设置cookie的!
第二次的client get:
出现:Cookie: PHPSESSID=b6d43eeccc1c4682ce37be790386e15e

得证!!!





作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/1608/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2009-2-5 19:27
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]