脚本宝典收集整理的这篇文章主要介绍了在PHP3中实现SESSION的功能(一),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
SESSION
函数库:session.inc.
PHP3
<?
PHP if (!isset($__session_inc__)){
$__session_inc__=1;
//require("cookie.inc.
PHP3");
# -------------------------------------------------------------------&nbs
p; # Session Management v1.0 21.6.1998
# (c) Wild Karl H
einz <kh.wild@wicom.at>
#
# This Include handle Session based
VARiable handling
#
# Please feel free and use
IT. If you make it more functional
# it would be nice to send me a copy.
#
# Don't f
orget -
MysqL_connect !
#
# The database structure
# Table structure for table 'session'
#
# CREATE TABLE session (
# id int(11) DEFAULT '0' NOT NULL auto_increment,
# sid v
Archar(20) DEFAULT '' NOT NULL,
# val blob,
# times timest
amp(14),
#
Primary KEY (id),
# KEY sid (sid),
#
unique sid_2 (sid)
# );
#
# You'll miss here a cron job to delete the old sessions
From db
# -------------------------------------------------------------------
// 请注意上面被注释掉的CREATE TABLE语句,
// 你需要在你所使用的
数据库上执行这条语句,
// 表名也可以不是session,那么就需要设置下面的$sess_table变量了。
// 此处你需要设置库名,和表名。
//
不过一般建议就使用session作为表名
$sess_db = 'dbname';
$sess_table = 'session';
# ----------------------------------------------------
# Session_CheckID - 检查、设置并返回 Session
-iD
# 参数
......: cookie保存时间(以分钟计)
# 也可不设置表示这个 cookie 只在当前session 有效
# 这其实就象ASP中SESSION的时效一样。
# 返回值....:
一个唯一的Session-ID (作为cookie存储)
# ----------------------------------------------------
function Session_CheckID( $min )
{
global $sess_sid;
if( !$sess_sid ) {
$sess_sid = uniqid( SC ); //取得
一个唯一的
随机数
/*
if( $min > 0 ) {
SetCookie("sess_sid",$sess_sid,time()+($min*60),"/","",0 );
}
else {
SetCookie("sess_sid",0 );
}
上面是原先的
代码,会出错。所以另外用了
一个更好的
函数。
函数库:cookie.inc.
PHP3
*/
jssetcookie("sess_sid",$min);
return( false );
}
else {
return( true );
}
}
# ----------------------------------------------------------
# str2arr - 将
字符串转换成session数组
# 参数.....: string
# 返回值...: 全局数组(其实就是session)
#本
函数用途:将
字符串转换成session数组
#如"session[username]=yourid&session[userpass]=12345"
#将会被转换成下面的数组
# session[username]="yourid"
# session[userpass]="12345"
#请注意
函数split(),
each(),list(),eval()的
用法。
# ----------------------------------------------------------
function str2arr( $ts )
{
global $session;
$vals = split( "&",$ts );
while( list($key,$val) = each($vals) ) {
list( $name,$wert ) = split( "=",$val );
if( $val ) eval( "$$name = "$wert";" );
}
}
# ----------------------------------------------------------
# session_read() - 从SESSION表中取数据,转换成session数组
# 参数........: 无
# 返回值......: 如果读出数据,返回 true ,否则返回 false
#注意.........: 用到了str2arr()这个
函数 # ----------------------------------------------------------
function session_read()
{
# Hash array to keep session-variables
global $session;
global $sess_sid,$sess_db,$sess_table,$sess_error;
$sel = "Select val f
rom $sess_table where sid = '$sess_sid'";
$res =
MysqL_db_query( $sess_db,$sel );
if(
MysqL_numrows( $res ) ) {
$val =
MysqL_result( $res,"val" );
str2arr( $val );
MysqL_free_result( $res );
return( true );
}
else {
return( false );
$sess_error =
MysqL_error();
}
}
# ------------------------------------------------------
# Split_Array() - 将session数组转换成字符串
# 参数.......: 数组
# 返回值.....: 数组转换得来的字符串
#
# Thanks to Rasmus (这人好象是
PHP的发明人)
# 注意:将session数组转换成字符串
#如session[username]="yourid"
# session[userpass]="12345"
#将会被转换成"session[username]=yourid&session[userpass]=12345"
#同时该
函数考虑到了数组的某个元素也是数据的情况
#这个
函数被设计成
一个递归
函数 # ------------------------------------------------------
function Split_Array( $arr,$a = "",$b = "",$c = "" )
{
while( list( $key,$val ) = each( $arr ) ) {
if( is_array( $val ) ) {
$ts .= Split_Array( $arr[ $key ],
(
strlen( $a ) ? $a : $key ),
( strlen( $b ) ? $b : ( strlen( $a ) ? $key : "" ) ),
( strlen( $c ) ? $c : ( strlen( $b ) ? $key : "" ) ) );
}
else {
$ts .= "session";
$ts .= $a ? "[$a]" : "";
$ts .= $b ? "[$b]" : "";
$ts .= $c ? "[$c]" : "";
$ts .= "[$key]=$val&";
}
}
return( $ts );
}
# ---------------------------------------------------
# session_write - 将session数组转换成字符串,再存到session表中
# 参数.: 无
# 返回值...: 如果存入
正常返回 true ,否则返回 false
# ---------------------------------------------------
function session_write()
{
# Hash array to keep session-variables
global $session;
global $sess_sid,$sess_table;
global $sess_error;
# if you like to delete a session-cookie
# you must check it before writting the session
# array
if( !$sess_sid ) { session_checkid( 0 ); }
$ts = Split_Array( $session );
if( $ts > "" ) { $ts = s
ubstr( $ts,strlen( $ts ) - 1 ); }
$res =
MysqL_db_query( $sess_db,"Select * from session where sid = '$sess_s'");
if(
MysqL_numrows( $res )
== 0 ) {
$sel = "Insert into $sess_table ( id,sid,val,times ) ";
$sel .= "values( 0,'$sess_sid','$ts',NULL )";
}
else {
$sel = "Update $sess_table set val = '$ts',";
$sel .= "times = NULL where sid = '$sess_sid'";
}
if( !
MysqL_db_query( $sess_db,$sel ) ) {
$sess_error =
MysqL_error();
return( false );
}
else { return( true ); }
}
# ---------------------------------------------
# session_del - 清除当前所有的session
# 并
删除session表中和当前session有关的记录
# 参数.....:
一个随机的session id
# 返回值...: 无
# ---------------------------------------------
function session_del()
{
global $session,$sess_sid;
$sel = "Delete from $sess_table where sid = '$sess_sid'";
if( !
MysqL_db_query( $sess_db,$sel ) ) {
$sess_error =
MysqL_error();
}
$sess_sid = '';
}
}
?>
原作者:不详
脚本宝典总结
以上是脚本宝典为你收集整理的在PHP3中实现SESSION的功能(一)全部内容,希望文章能够帮你解决在PHP3中实现SESSION的功能(一)所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。