1,session入库
<?php
//session入库
function sess_open(){
echo __FUNCTION__;
$link = mysql_connect('127.0.0.1', 'root', 'root');
mysql_query('set names utf8');
mysql_query('use practice');
}
function sess_close(){
echo __FUNCTION__;
}
function sess_read($sess_id){
echo __FUNCTION__;
$sql = "select sess_data from `session` where sess_id = '$sess_id'";
$result = mysql_query($sql); // $link 可以自己找到,或可以声明为全局变量
if($rows = @mysql_fetch_assoc($result)){
return $rows['sess_data'];
}else{
return '';
}
}
function sess_write($sess_id, $sess_data){
echo __FUNCTION__;
//当前 session 存在则更新 sess_data
//获得时间戳,mysql函数:unix_timestamp();
//获得时间戳,php函数:time();
$sql = "replace into session(sess_id,sess_data,time) values('".$sess_id."','".$sess_data."',".time().")"; //这是为了gc()
return mysql_query($sql);
}
function sess_destroy($sess_id){
echo __FUNCTION__;
$sql = "delete from `session` where sess_id = '$sess_id'";
return mysql_query($sql);
}
function sess_gc(){
echo __FUNCTION__;
}
session_set_save_handler(
'sess_open',
'sess_close',
'sess_read',
'sess_write',
'sess_destroy',
'sess_gc'
);
session_start();
//session_destroy();
// $_SESSION['name'] = 'f345454';
$_SESSION['age'] = '24';
var_dump($_SESSION);
?>
2.session 入redis
<?php
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://localhost:6379");
session_start();
header("Content-type:text/html;charset=utf-8");
$_SESSION['view'] = 'zhangsan';
//session_destroy();
var_dump($_SESSION);
//echo $_SESSION['view'];
//echo session_id();
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
//redis用session_id作为key并且是以string的形式存储
echo $redis->get('PHPREDIS_SESSION:' . session_id());
?>
或者用Session_set_save_handler(‘open’,’close’,’ read’,’ write’,’ destory’,’ gc’);
<?php
class RedisSession{
private $_redis = array(
'handler' => null, //数据库连接句柄
'host' => null, //redis端口号
'port' => null,
);
public function __construct($array = array()){
isset($array['host'])?$array['host']:"false";
isset($array['port'])?$array['host']:"false";
$this->_redis = array_merge($this->_redis, $array);
}
public function begin(){
//设置session处理函数
session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destory'),
array($this, 'gc')
);
}
public function open(){
$redis = new Redis();
$redis->connect($this->_redis['host'], $this->_redis['port']);
if(!$redis){
return false;
}
$this->_redis['handler'] = $redis;
$this->gc(null);
return true;
}
//关
public function close(){
return $this->_redis['handler']->close();
}
//读
public function read($session_id){
return $this->_redis['handler']->get($session_id);
}
//写
public function write($sessionId, $sessionData){
return $this->_redis['handler']->set($sessionId, $sessionData);
}
public function destory($sessionId){
return $this->_redis['handler']->delete($sessionId) >= 1 ? true : false;
}
public function gc(){
//获取所有sessionid,让过期的释放掉
$this->_redis['handler']->keys("*");
return true;
}
}
$ses = new RedisSession(array('host'=>'127.0.0.1','port'=>'6379'));
$ses->begin();
session_start();
$_SESSION['name']='zhangsan';
echo $_SESSION['name'];