php 从会话存储文件中读取会话数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4698432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:01:35  来源:igfitidea点击:

Read the session data from session storage file

phpserialization

提问by Shakti Singh

Facing problem with PHP unserialize()function as titled it is throwing error.

面临PHP unserialize()标题为“抛出错误”的函数的问题。

unserialize() [function.unserialize]: Error at offset 0 of 1781 bytes

I also tried the session_decode()which return bool(false)

我也试过session_decode()which 返回bool(false)

magic_quotes_gpcis Off.

magic_quotes_gpcOff

Well, I am reading content of file which is serialized. File contents looks like below.

好吧,我正在阅读已序列化的文件内容。文件内容如下所示。

core|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0:"";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12:"";b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20:"^@*^@_lastAddedMessage";N;}}customer|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0:"";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12:"";b:1;}s:19:"wishlist_item_count";i:0;}catalog|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0:"";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12:"";b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20:"^@*^@_lastAddedMessage";N;}}checkout|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0:"";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12:"";b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20:"^@*^@_lastAddedMessage";N;}}

核心|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0: "";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9 .2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12:"";b:1;}s:8:"messages";O:34 :"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20:"^@*^@_lastAddedMessage";N;}}customer|a:3: {s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0:"";s:20 :"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12:"" ;b:1;}s:19:"wishlist_item_count";i:0;}目录|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15 :"117.241.113.248";s:8:"http_via";s:0:"";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent";s:90:" Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1:{s:12: "";b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20: "^@*^@_lastAddedMessage";N;}}checkout|a:3:{s:23:"_session_validator_data";a:4:{s:11:"remote_addr";s:15:"117.241.113.248";s:8:"http_via";s:0:"";s:20:"http_x_forwarded_for";s:0:"";s:15:"http_user_agent" ;s:90:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";}s:13:"session_hosts";a:1 :{s:12:"";b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0: {}s:20:"^@*^@_lastAddedMessage";N;}}b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20:"^@ *^@_lastAddedMessage";N;}}b:1;}s:8:"messages";O:34:"Mage_Core_Model_Message_Collection":2:{s:12:"^@*^@_messages";a:0:{}s:20:"^@ *^@_lastAddedMessage";N;}}

my PHP code is below

我的 PHP 代码如下

$file='/var/www/html/products/var/session/sess_0ehb7ek0hmunqo3kq70t0t6mb0';
$contents=file_get_contents($file);
$data = unserialize($contents); 
var_dump($data);

I already tried the stripslashes()before unserializing data. Not sure where is the problem in data. I can not change the mechanism of storing data in to file because this is handled by Magento for mananging session on File level.

stripslashes()在反序列化数据之前,我已经尝试过。不确定数据的问题在哪里。我无法更改将数据存储到文件中的机制,因为这是由 Magento 处理的,用于在文件级别管理会话。

回答by StasM

If you want to decode session data, use session_decode(see the manual). unserializeonly decodes single variables, not session data.

如果要解码会话数据,请使用session_decode(请参阅手册)。unserialize只解码单个变量,而不是会话数据。

You can do something like:

您可以执行以下操作:

$file = '/var/www/html/products/var/session/sess_ciktos8icvk11grtpkj3u610o3';
$contents = file_get_contents($file);
session_start();
session_decode($contents);
print_r($_SESSION);

回答by Ahmet Erkan ?EL?K

Use this class:

使用这个类:

    <?php
class Session {
    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;
    }
}
?>

Usage:

用法:

<?php
Session::unserialize(file_get_contents($sessionSavePath."/".$sessionFileName);
?>

Thats Work!

这就是工作!

回答by Charles

That is not legal PHP serialized data, that's PHP session data.

这不是合法的 PHP 序列化数据,而是 PHP 会话数据。

PHP session data uses the serialized format internally, but it is notserialized data itself.

PHP 会话数据在内部使用序列化格式,但它本身不是序列化数据。

The only thing that can safely and sanely read session data is PHP's session code. It is sometimespossible to read it using a regular expression and some creative editing, but you can not rely upon those methods.

唯一可以安全、理智地读取会话数据的是 PHP 的会话代码。它有时可以使用正则表达式和一些创造性的编辑读它,但你不能依靠这些方法。

If you need data out of a user's session, your best bet is to write a custom session wrapper and let it do the work when the data itself changesrather than try and work with the data after the fact.

如果您需要用户会话中的数据,最好的办法是编写一个自定义会话包装器,并让它在数据本身发生变化时完成工作,而不是事后尝试处理数据。

(I'm not talking about custom session-writing code, I'm talking about a class that you would use instead of using $_SESSIONdirectly.)

(我不是在谈论自定义会话编写代码,而是在谈论您将使用而不是$_SESSION直接使用的类。)

回答by Prasanna

Following can be a way to read session data from session file

以下可以是从会话文件中读取会话数据的一种方式

//$file='/var/www/html/products/var/session/sess_ciktos8icvk11grtpkj3u610o3';
$sSessId = 'ciktos8icvk11grtpkj3u610o3';
session_id($sSessId);
session_start();
print_r($_SESSION);

回答by Prasanna

check out, this might click you something

退房,这可能会点击你的东西

function read($filename)
{
    session_save_path("/tmp/tst");
    session_start();
    echo    $sCurrentFile = "/tmp/tst/sess_".session_id();
    $sFileToRead = $filename;
    if( !file_exists($sFileToRead) || !$sessionData=(string)@file_get_contents($sFileToRead) )
    {
        echo "file does not exist";
    }

    $fh = fopen($sCurrentFile, 'w') or die("can't open file");
    fwrite($fh, $sessionData);
    fclose($fh);
    $_SESSION["mytest"] = 444; 
    print_r($_SESSION);
}