将session存入数据库,memcache的方法
//存入数据库
<?php
if(!$con = mysql_connect(‘localhost‘,‘root‘,‘123456‘)){
die(‘连接数据库失败‘);
}
$link = mysql_select_db(‘session‘);
//session入库
function open($save_path,$session_name){
return true;
}
function close(){
}
function read($id){
//查询的sql语句
if($result = mysql_query("select * from session where id=‘{$id}‘")){
if($row = mysql_fetch_assoc($result)){
return $row[‘data‘];
}
}else{
return ‘‘;
}
}
function write($id,$sess_data){
$expire = time();
$query = "insert into session (id,data) values (‘{$id}‘,‘{$sess_data}‘)
on duplicate key update data = ‘{$sess_data}‘";
if(mysql_query($query)){
return true;
}else{
return false;
}
}
function destroy($id)
{
if($result = mysql_query("DELETE * FROM session WHERE id=‘$id‘"))
{
return true;
}
else
{
return false;
}
}
function gc($maxlifetime)
{
return true;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
$_SESSION[‘tang‘]=‘5687678‘;
//存入memcache
<?php
ini_set(‘session.save_handler‘,‘memcache‘);
ini_set(‘session.save_path‘,‘tcp://127.0.0.1:11211‘);
session_start();
class Hot{
public $name;
public $color;
public function __construct($name,$color){
$this->name=$name;
$this->color=$color;
}
}
$hot=new Hot(‘xiaobei‘,‘white‘);
$_SESSION[‘hot‘]=$hot;
?>
Get_ini_session.php
<?php
ini_set(‘session.save_handler‘,‘memcache‘);
ini_set(‘session.save_path‘,‘tcp://127.0.0.1:11211‘);
session_start();
class Hot{
public $name;
public $color;
public function __construct($name,$color){
$this->name=$name;
$this->color=$color;
}
}
$hot=$_SESSION[‘hot‘];
var_dump($hot);
?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。