zoukankan      html  css  js  c++  java
  • 反序列化存入数据库里面的session数据

    session数据存取的方法可通过session.serialize_handler方法来判断,反序列化可通过下面的unserialize方法,参考http://stackoverflow.com/questions/27813619/unserializing-php-session-data-from-db-table

    public static function unserialize($session_data) {
    $method = ini_get("session.serialize_handler");
    switch ($method) {
    case "php":
    return self::unserialize_php($session_data);
    break;
    case "php_binary":
    return self::unserialize_phpbinary($session_data);
    break;
    default:
    throw new Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary");
    }
    }

    private static function unserialize_php($session_data) {
    $return_data = array();
    $offset = 0;
    while ($offset < strlen($session_data)) {
    if (!strstr(substr($session_data, $offset), "|")) {
    throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
    }
    $pos = strpos($session_data, "|", $offset);
    $num = $pos - $offset;
    $varname = substr($session_data, $offset, $num);
    $offset += $num + 1;
    $data = unserialize(substr($session_data, $offset));
    $return_data[$varname] = $data;
    $offset += strlen(serialize($data));
    }
    return $return_data;
    }

    private static function unserialize_phpbinary($session_data) {
    $return_data = array();
    $offset = 0;
    while ($offset < strlen($session_data)) {
    $num = ord($session_data[$offset]);
    $offset += 1;
    $varname = substr($session_data, $offset, $num);
    $offset += $num;
    $data = unserialize(substr($session_data, $offset));
    $return_data[$varname] = $data;
    $offset += strlen(serialize($data));
    }
    return $return_data;
    }
  • 相关阅读:
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    String详解
    数据库中索引相关基础知识
    论文笔记:RankIQA
    目标检测 | 火焰烟雾检测论文(实验部分)
    图像质量评价:合成失真图像方法
  • 原文地址:https://www.cnblogs.com/xuxiang/p/5320178.html
Copyright © 2011-2022 走看看