php自定義session示例分享
更新時(shí)間:2014年04月22日 11:26:18 作者:
這篇文章主要介紹了php自定義session示例,需要的朋友可以參考下
下面為session類的代碼
復(fù)制代碼 代碼如下:
<?php
class session
{
static function init()
{
session_set_save_handler(
array("session","open"),
array("session","close"),
array("session","read"),
array("session","write"),
array("session","destroy"),
array("session","gc")
);
}
static function open($save_path,$session_name)
{
echo "session opening!<br>";
/*global $db,$REMOTE_ADDR;
$rs = $db->Execute("select * from Sessions where SessionID='".session_id()."'");
$arry=$rs->FetchRow();
if( $rs && $arry)
{
$db->Execute("update Sessions set SessionLast=NOW() where SessionID='".session_id()."'");
}
else
{
$query = "insert into Sessions set SessionID='".session_id()."',SessionName='$REMOTE_ADDR',SessionLast='NOW()'";
//echo $query;
$db->Execute($query);
}*/
return true;
}
static function close()
{
return(true);
}
static function read($id)
{
echo "session reading now!<br>";
global $db;
return true;
$timenow = strftime("%Y-%m-%d %H:%M:%S", time());
$query = "select SessionData from Sessions where SessionID='$id' and SessionLast > '$timenow'";
$rs = $db->Execute($query);
if(list($SessionData) = $rs->FetchRow())
{
//echo $SessionData;
return $SessionData;
}
else
{
return false;
}
}
static function write($id,$sess_data)
{
echo "session writing now!<br>";
global $db;
$rs = $db->Execute("select SessionID from Sessions where SessionID='$id'");
$num = $rs->RecordCount();
$unix_time = time()+MY_SESS_TIME;
//echo MY_SESS_TIME;
$dateleft = strftime("%Y-%m-%d %H:%M:%S", $unix_time);
if($num <= 0)
{
$sql = "insert into Sessions set SessionData='$sess_data', SessionName='".$_SERVER["REMOTE_ADDR"]."', SessionLast='$dateleft', SessionID='".session_id()."'";
}
else
{
$sql = "update Sessions set SessionData='$sess_data', SessionName='".$_SERVER["REMOTE_ADDR"]."', SessionLast='$dateleft' where SessionID='$id'";
}
$db->Execute($sql);
}
static function destroy($id)
{
echo "session destroying now!<br>";
global $db;
$sql = "DELETE FROM Sessions WHERE `SessionID` = '$id'";
$rs = $db->Execute($sql);
return $rs;
// $sess_file = "$sess_save_path/sess_$id";
//return(@unlink($sess_file));
}
/*********************************************
* WARNING - You will need to implement some *
* sort of garbage collection routine here. *
*********************************************/
static function gc($maxlifetime)
{
echo "session maxlifetime now!<br>";
global $db;
$timenow = strftime("%Y-%m-%d %H:%M:%S", time());
$sql = "DELETE FROM `$table_sessions` WHERE `SessionLast` < '$timenow'";
return $sess_db->Execute($sql);
//echo "now gc!<br>";
return true;
}
// proceed to use sessions normally
}
使用方法
復(fù)制代碼 代碼如下:
include("session.class.php");
session::init();
session_start();
define("MY_SESS_TIME", 3600); //SESSION 生存時(shí)長
$_SESSION["test"] = "abcdef";
您可能感興趣的文章:
- php創(chuàng)建session的方法實(shí)例詳解
- php中http與https跨域共享session的解決方法
- php自定文件保存session的方法
- php通過session防url攻擊方法
- PHP自定session保存路徑及刪除、注銷與寫入的方法
- PHP實(shí)現(xiàn)利用MySQL保存session的方法
- PHP會(huì)話控制:Session與Cookie詳解
- php使用$_POST或$_SESSION[]向js函數(shù)傳參
- PHP利用MySQL保存session的實(shí)現(xiàn)思路及示例代碼
- php實(shí)現(xiàn)session自定義會(huì)話處理器的方法
相關(guān)文章
php模擬ping命令(php exec函數(shù)的使用方法)
使用php模擬我們常用的DOS命令ping命令的方法,這中間用到了exec函數(shù)并做函數(shù)解釋,還有相關(guān)函數(shù)system的使用。2013-10-10
Zend Framework教程之Application和Bootstrap用法詳解
這篇文章主要介紹了Zend Framework教程之Application和Bootstrap用法,結(jié)合實(shí)例形式詳細(xì)分析了Application和Bootstrap的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
Laravel 5框架學(xué)習(xí)之表單驗(yàn)證
Laravel 通過 Validation 類讓您可以簡單、方便的驗(yàn)證數(shù)據(jù)正確性及查看相應(yīng)的驗(yàn)證錯(cuò)誤信息。如果是更復(fù)雜的驗(yàn)證場景,你可能需要?jiǎng)?chuàng)建一個(gè)"表單請(qǐng)求"。表單請(qǐng)求是一個(gè)自定義的請(qǐng)求類包含了一些驗(yàn)證的邏輯。你可以通過 Artisan 的命令行 make:request 來創(chuàng)建一個(gè)表單請(qǐng)求類2015-04-04
再談Yii Framework框架中的事件event原理與應(yīng)用
這篇文章主要介紹了再談Yii Framework框架中的事件event原理與應(yīng)用,結(jié)合實(shí)例形式分析了再談Yii框架中的事件event相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
thinkphp框架無限級(jí)欄目的排序功能實(shí)現(xiàn)方法示例
這篇文章主要介紹了thinkphp框架無限級(jí)欄目的排序功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkphp無限級(jí)欄目排序相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
php 提速工具eAccelerator 配置參數(shù)詳解
php 提速工具eAccelerator 配置參數(shù)詳解,需要的朋友可以參考下。2010-05-05

