zl程序教程

您现在的位置是:首页 >  其它

当前栏目

php4的session功能评述(一)

功能 session php4 评述
2023-06-13 09:13:44 时间
php4比php3新加了session的支持。稍微用了一下,对其函数接口,内部机制,  
应用的方便性做了大概的了解。  
session的意义大家都应该清楚,一个session可以包括数次http的请求和应答,  
比如我们用163.net,从login到logout或者超时就作为一个session,session  
的唯一标识一般是在系统内部生成一个唯一的sessionID,一般是一个挺长的  
字符串。一个session除了sessionID,还可以有自己的sessiondata,可以  
记录和区分sesion的不同状态。  

php4对session操作提供以下接口:  

session_start—Initializesessiondata  
session_destroy—Destroysalldataregisteredtoasession  
session_name—Getand/orsetthecurrentsessionname  
session_module_name—Getand/orsetthecurrentsessionmodule  
session_save_path—Getand/orsetthecurrentsessionsavepath  
session_id—Getand/orsetthecurrentsessionid  
session_register—Registeravariablewiththecurrentsession  
session_unregister—Unregisteravariablefromthecurrentsession  
session_is_registered—Findoutifavariableisregisteredinasession  
session_decode—Decodessessiondatafromastring  
session_encode—Encodesthecurrentsessiondataasastring  

意义大家一看就能明白,session_start开始一个session,session_destroy结  
束一个session,session_id取得当前的session_id,session_register向当前  
的session注册一个变量,这个很有用,比如用户逛商场,选中了某几样商品你  
就可以用session_register把商品名称或者代码register到当前的session中。  

比如下面例子(摘自phpmanual):  

<?php  
session_register("count");  
$count++;  
?>  

Hellovisitor,youhaveseenthispage<?echo$count;?>times.<p>  
#the<?=SID?>isnecessarytopreservethesessionid
#inthecasethattheuserhasdisabledcookies

Tocontinue,<AHREF="nextpage.php?<?=SID?>">clickhere</A>

session_register可以隐式地激发session_start(如果用户之前没发session_
start调用),当前的session注册了一个变量count,每次用户点击clickhere
的时候,这个变量都会增一。你可以自己试一下。<?=SID?>的意义不多赘述。