主页 > 编程资料 > PHP >
发布时间:2014-12-06 作者:网络 阅读:111次

但是如果是在线人数多的时候 ,可以考虑用缓存服务器,其实有很多种思路。。

 

<?php
/*
 *@des 将session 存储到数据库内 实现共享 
 *@author wangjian
 *time 2013-05-07 
 */



//创建sql 表
####################################################
/*
 *CREATE TABLE `common_sessions` (
  `SessionKey` varchar(32) NOT NULL default '',
  `SessionArray` blob NOT NULL,
  `SessionExpTime` int(20) unsigned NOT NULL default '0',
  PRIMARY KEY  (`SessionKey`),
  KEY `SessionKey` (`SessionKey`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
 * 
 */
####################################################


#
class MysqlSession {       // 注意在有使用Session的页面。页面一定要顶格,页面开始处不能留空。
private $DB_SERVER = "localhost";   // 数据库服务器主机名
    private $DB_NAME = "ci";    // 数据库名字
    private $DB_USER = "root";     // MYSQL 数据库访问用户名 
    private $DB_PASS = "wangjian";      // MYSQL 数据库访问密码     
    private $DB_SELECT_DB = "";   //返回数据库连接的标示
	private $SESS_LIFE = 1440;
	private $table_name = 'common_sessions'; 
function MysqlSession () {
   $this->DB_NAME = $this->DB_NAME;
   session_module_name('user');//设置session的存储方式
   //ini_set("session.save_handler", "user");//设置session的存储方式
   session_set_save_handler( 
    array(&$this, 'sess_open'), 
    array(&$this, 'sess_close'), 
    array(&$this, 'sess_read'), 
    array(&$this, 'sess_write'), 
    array(&$this, 'sess_destroy'), 
    array(&$this, 'sess_gc') 
   );
   session_start();
}
     
function sess_open($save_path, $session_name){   // 打开数据库连接
   if (! $this->DB_SELECT_DB = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS)) { 
	    echo "SORRY! MYSQL ERROR : Can't connect to $this->DB_SERVER as $DB_USER"; 
	    echo "MySQL Error: ", mysql_error(); 
		 die; 
   } 
   
   if (! mysql_select_db($this->DB_NAME, $this->DB_SELECT_DB)) { 
    echo "SORRY! MYSQL ERROR : Unable to select database $this->DB_NAME"; 
    die; 
   } 
   return true; 
} 
   
function sess_close() { 
       return true; 
} 
   
function sess_read($SessionKey){   
        $Query = "SELECT SessionArray FROM {$this->table_name} WHERE SessionKey = '".$SessionKey."' AND SessionExpTime > " . time(); // 过期不读取。
        $Result = mysql_query($Query, $this->DB_SELECT_DB);
        if($Result){
           if (list($SessionArray) = mysql_fetch_array($Result)) { 
           
		        	 return $SessionArray; 
 			}      	
        } 
 
   return false; 
} 
   
function sess_write($SessionKey, $VArray) { 
	//echo 'write';
   	$SessionExpTime = time() + $this->SESS_LIFE; // 更新Session过期时间,Session过期时间 = 最后一次更新时间 + Session的最大使用时长
    $SessionArray = addslashes($VArray); 
/*    $Query_update = "UPDATE {$this->table_name} SET SessionExpTime = '".$SessionExpTime."', SessionArray = '".$SessionArray."' WHERE SessionKey = '".$SessionKey."' AND   SessionExpTime > " . time(); 
    mysql_query($Query_update,$this->DB_SELECT_DB);
    $num = mysql_affected_rows();
    if($num){
    	return true ;
    }else{
    	//插入
    	$Query = "INSERT INTO {$this->table_name} (SessionKey,SessionExpTime,SessionArray) VALUES ('".$SessionKey."','".$SessionExpTime."','".$SessionArray."')"; 
    	mysql_query($Query, $this->DB_SELECT_DB); 
    	return true ;
    }
    */
    $replace_into = "REPLACE INTO {$this->table_name}(SessionKey,SessionExpTime,SessionArray) VALUES('".$SessionKey."','".$SessionExpTime."','".$SessionArray."')" ;
    return mysql_query($replace_into, $this->DB_SELECT_DB);
 /*   $Query = "INSERT INTO {$this->table_name} (SessionKey,SessionExpTime,SessionArray) VALUES ('".$SessionKey."','".$SessionExpTime."','".$SessionArray."')"; 
   
    $Result = mysql_query($Query, $this->DB_SELECT_DB); 
    if (!$Result){ 
           $Query = "UPDATE {$this->table_name} SET SessionExpTime = '".$SessionExpTime."', SessionArray = '".$SessionArray."' WHERE SessionKey = '".$SessionKey."' AND   SessionExpTime > " . time(); 
           $Result = mysql_query($Query, $this->DB_SELECT_DB); 
    }    
    return $Result; */
} 
    
function sess_destroy($SessionKey) { 
	
   $Query = "DELETE FROM {$this->table_name} WHERE SessionKey = '".$SessionKey."'"; 
   $Result = mysql_query($Query, $this->DB_SELECT_DB); 
   return $Result; 
} 
function __destruct() { 

   $Query = "DELETE FROM {$this->table_name} WHERE SessionExpTime <".time(); 
  $Result = mysql_query($Query, $this->DB_SELECT_DB); 
   return $Result; 
} 
   
function sess_gc($maxlifetime) { // 这个垃圾清除器系统调用。默认是1440秒清除一次(24分钟)。参数可以在PHP.ini里面设置。
   $Query = "DELETE FROM {$this->table_name} WHERE SessionExpTime < " . time(); 
   $Result = mysql_query($Query, $this->DB_SELECT_DB); 
   return mysql_affected_rows($this->DB_SELECT_DB); 
}

   
}
$session = new  MysqlSession();



###########################################################

#设置session 
//require './Session.class.php';
//$_SESSION['name'] = 'wangjian';
###########################################################

#读取session
//require './Session.class.php';
//echo $_SESSION['name'];
//print_r($_SESSION);

#############################################################
#销毁session
#require './Session.class.php';
#session_destroy() ; //调用了sess_destroy方法和__destruct方法
#############################################################
?>
代码下载地址:http://57sy.com/content/uploadfile/201305/df169899f861ebd879f50b3c7fe0e81c20130508095959.zip
关键字词: