ECSHOP session

时间:2024-01-14 20:22:20

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

; // SESSION 过期时间

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

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

;

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']);
            }
    }

;
        $userid  ;

/************************************************************************
       *  用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');

;
    
         header("Location: e.php");     //在e.php再加载一次cls_session类,输出session变量
}
?>