【问题标题】:Error on unserialize()反序列化()错误
【发布时间】:2018-01-22 11:45:04
【问题描述】:

我正在尝试修复 unserialize() 上的 PHP 错误。我知道我们可以使用@ 来抑制它,但是否可以在不抑制的情况下修复该错误。

这是表格和我的代码:

TABLE: 'config'

id   c_key                  c_value
1    facebook               a:1:{i:0;s:8:"Newsfeed";}
2    mg_notification_msv    to.aaaa.org

public function db_get_config($key, $default = null)
{
    if (!empty($key)) {
        // $record = $this->db_get_record($this->tables['config'], array('c_key' => $key));
        $record = $this->db->from($this->tables['config'])->where(array('c_key' => $key))->limit(1)->get()->row_array();
        if (!empty($record)) {
            $value = unserialize($record['c_value']); // Message: unserialize(): Error at offset 0 of 15 bytes
            if ($value === false) {
                $value = $record['c_value'];
            }
            return $value;
        }
    }

    return $default;
}

当将$key 作为facebook 或mg_notification_msv 传递时,函数在var_dump() 上返回array(1) { [0]=> string(8) "Newsfeed" } 或string(15) "to.aaaa.org"。

这会返回错误:

遇到 PHP 错误 严重性:通知消息:unserialize():在 15 个字节的偏移量 0 处出错 文件名:models/common_model.php 行号:xxxx

有没有办法修复这个 PHP 错误?

在$record['c_value'] 上使用strlen() 对我没有帮助。

【问题讨论】:

  • PHP unserialize problem的可能重复
  • @PhilippMaurer 上面提到的类似问题并没有解决我的问题。我添加了代码功能检查serialized。感谢 Wordpress。

标签: php serialization


【解决方案1】:

它可能会抛出该错误,因为当您尝试反序列化 mg_notification_msv 的值时,它会尝试反序列化之前未序列化的字符串(to.aaaa.org 不是序列化字符串)

只有当字符串不可序列化时,您才可以尝试反序列化:

if (@unserialize($record['c_value']) !== false ) {
    $value = unserialize($record['c_value']);
} else {
    $value = $record['c_value'];
}

【讨论】:

  • 然后您可以尝试使用正则表达式的函数来检测字符串是否被序列化,就像 wordpress 使用的那样:pastebin.com/dKVnsWNz
【解决方案2】:

我尝试使用 WP is_serialized() 并可能解决了我的问题。

<?php
public function db_get_config($key, $default = null)
{
    if (!empty($key)) {
        // $record = $this->db_get_record($this->tables['config'], array('c_key' => $key));
        $record = $this->db->from($this->tables['config'])->where(array('c_key' => $key))->limit(1)->get()->row_array();
        if (!empty($record)) {
            $e = $this->is_serialized($record['c_value']); // WP support function
            if ($e === true) {
                $value = unserialize($record['c_value']);
                if ($value === false) {
                    $value = $record['c_value'];
                }
            } else {
                $value = $record['c_value'];
            }
            return $value;
        }
    }

    return $default;
}

private function is_serialized( $data, $strict = true )
{
    // if it isn't a string, it isn't serialized.
    if ( ! is_string( $data ) ) {
        return false;
    }
    $data = trim( $data );
    if ( 'N;' == $data ) {
        return true;
    }
    if ( strlen( $data ) < 4 ) {
        return false;
    }
    if ( ':' !== $data[1] ) {
        return false;
    }
    if ( $strict ) {
        $lastc = substr( $data, -1 );
        if ( ';' !== $lastc && '}' !== $lastc ) {
            return false;
        }
    } else {
        $semicolon = strpos( $data, ';' );
        $brace     = strpos( $data, '}' );
        // Either ; or } must exist.
        if ( false === $semicolon && false === $brace )
            return false;
        // But neither must be in the first X characters.
        if ( false !== $semicolon && $semicolon < 3 )
            return false;
        if ( false !== $brace && $brace < 4 )
            return false;
    }
    $token = $data[0];
    switch ( $token ) {
        case 's' :
        if ( $strict ) {
            if ( '"' !== substr( $data, -2, 1 ) ) {
                return false;
            }
        } elseif ( false === strpos( $data, '"' ) ) {
            return false;
        }
        // or else fall through
        case 'a' :
        case 'O' :
        return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
        case 'b' :
        case 'i' :
        case 'd' :
        $end = $strict ? '$' : '';
        return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data );
    }
    return false;
}
?>

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 2014-03-13
    • 2020-11-08
    • 2014-05-03
    • 2018-07-28
    相关资源
    最近更新 更多