蓝色加粗,通过系统内核函数open调用打开session文件,这是由session_start()产生的调用,
  注意这里并没有产生读文件操作。红色部分,将一个空字符串写入session文件。
  由此可见session初始化在页面生命周期开始之时,手工调用session_start可以初始化session文件,
  而在页面生命周期结束之时,会自动地注销session,结束当前session生命周期,
  同时在此周期产生的session数据写回session文件,我们把这种方式结束的session,称为session默认结束。
  任务2:  观察session_register()查看它是否会产生磁盘操作,还是只操作$_SESSION。
<?php
//@file test_session_2.php
session_start();
$pg_uuid = 'ac606826-9620-490b-b850-ea9dbce6cfd5';
session_register('pg_uuid'); //注册全局变量pg_uuid到session,但值为NULL,只影响$_SESSION
var_dump($_SESSION);
fopen(__FILE__, "r");
[root@localhost ~]# strace -p `cat /var/run/httpd.pid`
Process 21819 attached - interrupt to quit
...
open("/var/www/html/test_session_2.php", O_RDONLY) = 17
fstat64(17, {st_mode=S_IFREG|0644, st_size=148, ...}) = 0
lseek(17, 0, SEEK_CUR)                  = 0
read(17, "<?php session_start(); $pg_uuid "..., 8192) = 148
read(17, "", 8192)                      = 0
read(17, "", 8192)                      = 0
close(17)                               = 0
open("/var/lib/php/session/sess_4j38nv7l1fq1bj6n80l6g9cum5", O_RDWR|O_CREAT, 0600) = 17
flock(17, LOCK_EX)                      = 0
fcntl64(17, F_SETFD, FD_CLOEXEC)        = 0
fstat64(17, {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
time(NULL)                              = 1270907613
open("/var/www/html/test_session_2.php", O_RDONLY) = 18
fstat64(18, {st_mode=S_IFREG|0644, st_size=148, ...}) = 0
lseek(18, 0, SEEK_CUR)                  = 0
close(18)                               = 0
chdir("/var/lib/php/session")           = 0
pwrite64(17, "pg_uuid|N;", 10, 0)       = 10
close(17)                               = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
writev(16, [{"HTTP/1.1 200 OK Date: Sat, 10 A"..., 328}, {"array(1) {   ["pg_uuid"]=>   NUL"..., 36}], 2) = 364
write(12, "192.168.0.98 - - [10/Apr/2010:21"..., 210) = 210
shutdown(16, 1 /* send */)              = 0
epoll_wait(15,
  通过上面的观察,蓝色部分还是由session初始化(session_start)产生,注意这里依然没读文件操作,这是因为session文件为空。红色部分,依然是默认结束session产生的文件写操作(pwrite)。由此,我们可以知道session_register()不会对session文件操作,即不会把$_SESSION中的数据写回session文件,它没有产生任何IO操作。而只在session生命周期是影响当前$_SESSION变量,即$_SESSION[‘pg_uuid’] = NULL。所以,推荐使用$_SESSION[‘pg_uuid’] = $pg_uuid;
  任务3:  观察session_destroy()与session_unset()的区别
<?php
session_start();
echo "<br/>---1--<br/>";
$pg_uid = 1;
//$_SESSION['pg_uid']; //该行会报一个Notice消息,即没有初始化该变量
$_SESSION['pg_name'] = 'boys'; //填入到$_SESSION变量,但不立即写入session文件,值为boys
$pg_sex = 1;
$pg_theme = 'default';
session_register('pg_sex'); //填入到$_SESSION变量,但不立即写入session文件,值为NULL
session_register('pg_theme'); //填入到$_SESSION变量,但不立即写入session文件,值为NULL
var_dump($_SESSION);
//--
echo "<br/>---2--<br/>";
unset($_SESSION['pg_theme']); //从$_SESSION清除该元素,不立即同步到session文件
unset($_SESSION['pg_name']); //从$_SESSION清除该元素,不立即同步到session文件
session_unregister('pg_sex'); //从$_SESSION清除该元素,不立即同步到session文件
session_unregister('pg_uid'); //从$_SESSION清除该元素,不立即同步到session文件
var_dump($_SESSION);
echo "<br/>---3--<br/>";
$_SESSION['pg_members'] = 5; //填入$_SESSION数组,但不立即同步到session文件,值为5
$pg_boy = 6;
session_register('pg_boy'); //填入$_SESSION数组,但不立即同步到session文件,值为NULL
session_unset($_SESSION); //清空$_SESSION
var_dump($_SESSION);
echo "<br/>---4--<br/>";
$_SESSION['pg_boss'] = 3; //填入$_SESSION数组,但不立即同步到session文件,值为3
$pg_girls = 6;
session_register('pg_girls'); //填入$_session数组,但不立即同步到session文件,值为NULL
session_destroy(); //注销session_destroy
var_dump($_SESSION);
echo "<br/>---5---<br/>";
session_unregister('pg_boss'); //pg_boss不会被清除,还为NULL
session_unset(); //不会清空$_SESSION数组,因为session已被session_destroy注销
var_dump($_SESSION);
fopen(__FILE__, "r");
//@这里是页面析构的时候-- 本应该将$_SESSION数据同步到session文件, 真的吗???
//@事实,没有发生任何IO操作,即没有将$_SESSION数据回写,怎么回事???
//@因为被session_destroy()消毁了session...
程序输出:
---1--
array(3) { ["pg_name"]=> string(4) "boys" ["pg_sex"]=> NULL ["pg_theme"]=> NULL }
---2--
array(0) { }
---3--
array(0) { }
---4--
array(2) { ["pg_boss"]=> int(3) ["pg_girls"]=> NULL }
---5---
array(2) { ["pg_boss"]=> int(3) ["pg_girls"]=> NULL }
[root@localhost ~]# strace -p `cat /var/run/httpd.pid`
Process 21819 attached - interrupt to quit
...
open("/var/www/html/test_session_3.php", O_RDONLY) = 17
fstat64(17, {st_mode=S_IFREG|0644, st_size=706, ...}) = 0
lseek(17, 0, SEEK_CUR)                  = 0
read(17, "<?php session_start(); echo "<br"..., 8192) = 706
read(17, "", 8192)                      = 0
read(17, "", 8192)                      = 0
close(17)                               = 0
open("/var/lib/php/session/sess_4j38nv7l1fq1bj6n80l6g9cum5", O_RDWR|O_CREAT, 0600) = 17
flock(17, LOCK_EX)                      = 0
fcntl64(17, F_SETFD, FD_CLOEXEC)        = 0
fstat64(17, {st_mode=S_IFREG|0600, st_size=10, ...}) = 0
pread64(17, "pg_uuid|N;", 10, 0)        = 10
close(17)                               = 0
unlink("/var/lib/php/session/sess_4j38nv7l1fq1bj6n80l6g9cum5") = 0
time(NULL)                              = 1270910665
open("/var/www/html/test_session_3.php", O_RDONLY) = 17
fstat64(17, {st_mode=S_IFREG|0644, st_size=706, ...}) = 0
lseek(17, 0, SEEK_CUR)                  = 0
close(17)                               = 0
chdir("/var/lib/php/session")           = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
...
write(12, "192.168.0.98 - - [10/Apr/2010:22"..., 211) = 211
shutdown(16, 1 /* send */)              = 0
  蓝色部分是我们熟悉的session初始化的时候产生的open系统内核调用。绿色部分,是一个IO读操作,因为上一次访问页面的时候,产生了session数据,所以这一次会将上次的session填入$_SESSION中。红色部分,可以看出,这里调用unlink删除session文件,而且后面(页面生命周期结束时),一直没有看到前两例看到的任何与session文件有关的IO写操作,即没有将$_SESSION中的数据写回session文件。我们也没有在session.save_path找到相应的session文件
  [root@localhost html]# ls /var/lib/php/session/sess_4j38nv7l1fq1bj6n80l6g9cum5  ls: /var/lib/php/session/sess_4j38nv7l1fq1bj6n80l6g9cum5: No such file or directory
  注意: 虽然删除了session文件,但用户再次访问web的时候,并不会给用户重新分配一个新的session id,而是依然用该session id,并且会重新创建文件名相同的session文件,即sess_SESSION-ID