SESSION函数库:session.inc.php3
<?php
if (!isset($session_inc)){
$session_inc=1;
//require("cookie.inc.php3");
// 请注意上面被注释掉的CREATE TABLE语句,
// 你需要在你所使用的数据库上执行这条语句,
// 表名也可以不是session,那么就需要设置下面的$sess_table变量了。
// 此处你需要设置库名,和表名。
// 不过一般建议就使用session作为表名
$sess_db = 'dbname';
$sess_table = 'session';
function Session_CheckID( $min )
{
global $sess_sid;
if( !$sess_sid ) {
$sess_sid = uniqid( SC ); //取得一个唯一的随机数
/
if( $min > 0 ) {
SetCookie("sess_sid", $sess_sid, time()+($min60), "/", "", 0 );
}
else {
SetCookie("sess_sid", $sess_sid, "", "/", "", 0 );
}
上面是原先的代码,会出错。所以另外用了一个更好的函数。
函数库:cookie.inc.php3
*/
jssetcookie("sess_sid",$sess_sid,$min);
return( false );
}
else {
return( true );
}
}
function str2arr( $ts )
{
global $session;
$vals = split( "&", $ts );
while( list($key,$val) = each($vals) ) {
list( $name, $wert ) = split( "=", $val );
if( $val ) eval( "$$name = "$wert";" );
}
}
function session_read()
{
global $session;
global $sess_sid, $sess_db, $sess_table, $sess_error;
$sel = "Select val from $sess_table where sid = '$sess_sid'";
$res = mysql_db_query( $sess_db, $sel );
if( mysql_numrows( $res ) ) {
$val = mysql_result( $res, 0, "val" );
str2arr( $val );
mysql_free_result( $res );
return( true );
}
else {
return( false );
$sess_error = mysql_error();
}
}
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 );
}
function session_write()
{
global $session;
global $sess_sid, $sess_db, $sess_table;
global $sess_error;
if( !$sess_sid ) { session_checkid( 0 ); }
$ts = Split_Array( $session );
if( $ts > "" ) { $ts = substr( $ts, 0, 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 ); }
}
function session_del()
{
global $session, $sess_db, $sess_table, $sess_sid;
$sel = "Delete from $sess_table where sid = '$sess_sid'";
if( !mysql_db_query( $sess_db, $sel ) ) {
$sess_error = mysql_error();
}
$sess_sid = '';
}
}
?>
原作者:不详