首頁>Program>source

面對問题 標题中的函式將引發錯誤。

PHP unserialize()

我也尝試了 unserialize() [function.unserialize]: Error at offset 0 of 1781 bytes 返迴 session_decode()

bool(false)magic_quotes_gpc

好,我正在讀取已序列化的檔案的內容.檔案內容如下所示。

Off

我的PHP代碼在下面

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;}}

我已经尝試過 $file='/var/www/html/products/var/session/sess_0ehb7ek0hmunqo3kq70t0t6mb0'; $contents=file_get_contents($file); $data = unserialize($contents); var_dump($data); 在反序列化資料之前。 不確定資料中的問题在哪裏。 我無法更改將資料儲存到檔案中的機製,因為這是由Magento處理的,用於在檔案級別管理会话。

stripslashes()
最新回復
  • 12天前
    1 #

    如果要解碼会话資料,請使用 session_decode (請參见手册). unserialize 仅解碼單个變數,不解碼会话資料.

    您可以執行以下操作:

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

  • 12天前
    2 #

    使用此類:

       <?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;
        }
    }
    ?>
    

    用法:

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

    那行得通!

  • 12天前
    3 #

    那不是合法的PHP序列化資料,而是PHP会话資料。

    PHP会话資料在內部使用序列化格式,但它不是序列化資料本身。

    唯一可以安全,合理地讀取会话資料的是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 changes 而不是尝試事後處理資料。

    (我不是在谈論自定義的会话編寫代碼,我是在谈論您將使用的類而不是使用 $_SESSION

  • 12天前
    4 #

    以下可能是从会话檔案中讀取会话資料的一種方法

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

  • 12天前
    5 #

    簽出,這可能会使您點击

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

  • command line:如何使用setenv()在C ++中匯出變數?
  • javascript:windoweventkeyCode如何在Firefox上執行此操作?