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;
    }
  • 相关阅读:
    android stagefright awesomeplayer 分析
    stagefright框架(七)-Audio和Video的同步
    stagefright框架(六)-Audio Playback的流程
    Windows Sockets Error Codes
    编译boost (windows msvc14)
    golang windows程序获取管理员权限(UAC ) via gocn
    阿里云容器服务--配置自定义路由服务应对DDOS攻击
    store / cache 系列
    一些项目感悟
    protobuf-3.0.0-beta-2 windows编译 x64/x86
  • 原文地址:https://www.cnblogs.com/xuxiang/p/5320178.html
Copyright © 2011-2022 走看看