将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);

?>

posted on 2014-07-18 15:50  面壁偷笑  阅读(333)  评论(0编辑  收藏  举报