zoukankan      html  css  js  c++  java
  • 将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);

    ?>

  • 相关阅读:
    day22 面向对象
    springMVC中 POST 请求数据变乱码问题
    Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract
    java.sql.SQLException: Unknown system variable 'tx_isolation'
    Mysql命令
    paas相关,添加ing
    Angular2中实现基于TypeScript的对象合并方法:extend()
    NgStyle和NgIf控制HTML标签显示的区别
    执行ng build --prod --aot命令报错
    JavaScript中的小陷阱(不定期更新。。)
  • 原文地址:https://www.cnblogs.com/tangchuanyang/p/3853683.html
Copyright © 2011-2022 走看看