<?
class CSession {
// ----------------------------------------------------------------------------
// 建構時自動判別該使用者是否來過,若未來過,就餵他吃小餅乾。
// ----------------------------------------------------------------------------
function CSession() {
global $cookie_sid;
if($cookie_sid != NULL) {
session_id($cookie_sid);
session_start();
}
else {
session_start();
$set_sid = session_id();
setcookie("cookie_sid", $set_sid, time()+3600*24*365); // 紀錄一年
//setcookie("cookie_sid", $set_sid); // 客戶離線後就部延續資料
}
}
// ----------------------------------------------------------------------------
// 登記一個變數在 Session 中。
// 範例: $this->setVar("aaa", "Hello Kitty");
// 則 $aaa = "Hello Kitty";
// ----------------------------------------------------------------------------
function setVar($name, $vars) {
global $$name;
session_start();
if(session_is_registered($name)) session_unregister($name);
session_register($name);
$$name = $vars;
}
// ----------------------------------------------------------------------------
// 取出 Session 的變數。
// 範例: $bbb = $this->getVar("aaa");
// 則會將曾經登錄過的變數內含值 $aaa 取出。
// ----------------------------------------------------------------------------
function getVar($name) {
session_start();
global $$name;
$return_str = FALSE;
if(session_is_registered($name) && $$name != "") $return_str = $$name;
return $return_str;
}
// ----------------------------------------------------------------------------
function clearAll() {
session_start();
session_unset();
// $there = session_encode();
// echo $there;
return TRUE;
}
// ----------------------------------------------------------------------------
function showAll() {
session_start();
$there = session_encode();
$here = split(";", $there);
return $here;
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
}
?>
使用時很簡單:
<? //從客戶表單輸入完後到這裡
include("CSession.php3");
$sess=new CSession;
$sid=session_id();
$sess->setVar("member_login","$login");
$sess->setVar("member_passwd","$password");
?>
任何時候要得到值:
$sess=new CSession;
$sid=session_id();
$m_id=$sess->getVar("member_login");
$m_pw=$sess->getVar("member_passwd");
|