XBOX

  PHP博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  13 随笔 :: 87 文章 :: 0 评论 :: 0 Trackbacks

<?php
class cls_session
{
    var $session_table  = '';

    var $max_life_time  = 1440; // SESSION 过期时间

    var $session_name   = '';
    var $session_id     = '';

    var $session_expiry = '';
    var $session_md5    = '';

    var $_time = 0;

    function __construct($session_table,$session_name){
        
       $this->cls_session($session_table,$session_name);
    }

    function cls_session($session_table,$session_name){

        $GLOBALS['_SESSION'] = array();
  
        $this->session_name       = $session_name;
        $this->session_table      = $session_table;
  
        if (!empty($_COOKIE[$this->session_name])){

            $this->session_id = $_COOKIE[$this->session_name];
        }
        else{

            $this->session_id = '';
        }      

        $this->_time = time();

        if ($this->session_id){

            $this->load_session();
        }
        else{
            $this->gen_session_id();

            setcookie($this->session_name, $this->session_id);
        }

        register_shutdown_function(array($this, 'update_session'));
    }

    function gen_session_id(){

        $this->session_id = md5(uniqid(mt_rand(), true));

        return $this->insert_session();
    }


    function insert_session(){

        return mysql_query('INSERT INTO ' . $this->session_table . " (sesskey, expiry,data) VALUES ('" . $this->session_id . "', ". $this->_time .",'a:0:{}')");
    }

    function load_session(){

        $result = mysql_query('SELECT data, expiry FROM ' . $this->session_table . " WHERE sesskey = '" . $this->session_id . "'");
  
       $session = mysql_fetch_array($result);
  
       if (!empty($session['data']) && $this->_time - $session['expiry'] <= $this->max_life_time){

                /***********************************************
               *   unserialize()还原已经序列化的SESSION变量              *
               *   保证了SESSION变量的页面间传值的作用               *
               ***********************************************/

                $GLOBALS['_SESSION']  = unserialize($session['data']);
            }
    }

    function update_session(){

        $adminid = !empty($GLOBALS['_SESSION']['admin_id']) ? intval($GLOBALS['_SESSION']['admin_id']) : 0;
        $userid  = !empty($GLOBALS['_SESSION']['user_id'])  ? intval($GLOBALS['_SESSION']['user_id'])  : 0;

        /************************************************************************
       *  用serialize函数将所有你所赋值的SESSION变量转化为一个序列化的字符串。        *
       *  SESSION本质不会发生变换,存入数据库中,用于下次调用。                                   *
       ************************************************************************/

 
        $data        = serialize($GLOBALS['_SESSION']);
        $this->_time = time();
       
        return mysql_query('UPDATE ' . $this->session_table . " SET expiry = " . $this->_time . ", userid = '" . $userid . "', adminid = '" . $adminid . "', data = '$data' WHERE sesskey = '" . $this->session_id . "'");
    }
}


// database host
$db_host   = "localhost";

// database name
$db_name   = "ecshop";

// database username
$db_user   = "root";

// database password
$db_pass   = "dingran";

$link = mysql_connect($db_host, $db_user, $db_pass);


if ($link){

 mysql_select_db($db_name);
}
else{

 exit('mysql connection error:'.mysql_error());
}


/* 初始化session */
$sess = new cls_session('ecs_sessions','ECSCP_ID');

/* 验证session */
if (!isset($_SESSION['admin_id'])){

         $_SESSION['admin_id'] = 9900099;
    
         header("Location: e.php");     //在e.php再加载一次cls_session类,输出session变量
}
?>

posted on 2008-07-17 10:46 XBOX 阅读(3154) 评论(0)  编辑 收藏 引用 网摘

只有注册用户登录后才能发表评论。
网站导航: