首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用带有对象的CodeIgniter 2.2会话时,取消序列化()偏移错误

使用带有对象的CodeIgniter 2.2会话时,取消序列化()偏移错误
EN

Stack Overflow用户
提问于 2015-06-26 22:41:53
回答 2查看 1.8K关注 0票数 1

我正在尝试调试CodeIgniter 2.2中的一些旧代码。在通过会话运行一些数据时,我注意到了一个非序列化错误,消息:un序列化():在163个字节的偏移量处出现了一个错误。在做了一些调试和研究之后,我发现在取消对来自会话的数据进行序列化时,存在一个常见的反斜杠问题。

我使用的序列化数据具有带有反斜杠的数据对象,这会导致错误发生。我需要一个可以处理标准类对象的替换。

有人能推荐一个快速替换代码点火器的会话_serialize()和_unserialize()方法吗?

代码语言:javascript
运行
复制
public function data_test() {

    $input = array(
        (object)array('name' => 'test2', 'desc' => 'bla bla ob/gyn'),
        (object)array('name' => 'test2', 'desc' => 'bla bla ob\\gyn'),
    );
    var_dump($input);

    $data = $this->_serialize($input);
    var_dump($data);

    $result = $this->_unserialize($data);
    var_dump($result);


}



// --------------------------------------------------------------------

/**
 * Serialize an array
 *
 * This function first converts any slashes found in the array to a temporary
 * marker, so when it gets unserialized the slashes will be preserved
 *
 * @access  private
 * @param   array
 * @return  string
 */
function _serialize($data) {
    if (is_array($data)) {
        foreach ($data as $key => $val) {
            if (is_string($val)) {
                $data[$key] = str_replace('\\', '{{slash}}', $val);
            }
        }
    } else {
        if (is_string($data)) {
            $data = str_replace('\\', '{{slash}}', $data);
        }
    }

    return serialize($data);
}

// --------------------------------------------------------------------

/**
 * Unserialize
 *
 * This function unserializes a data string, then converts any
 * temporary slash markers back to actual slashes
 *
 * @access  private
 * @param   array
 * @return  string
 */
function _unserialize($data) {

    $data = unserialize(strip_slashes($data));

    if (is_array($data)) {
        foreach ($data as $key => $val) {
            if (is_string($val)) {
                $data[$key] = str_replace('{{slash}}', '\\', $val);
            }
        }

        return $data;
    }

    return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
}
EN

Stack Overflow用户

回答已采纳

发布于 2015-06-29 19:07:39

代码语言:javascript
运行
复制
/**
 * Serialize an array
 *
 * This function serializes the data and then base64_encodes it for 
 * storage with memcached. This avoids the common backslash issue.
 *
 * @access  private
 * @param   array
 * @return  string
 */
function _serialize($data) {
    return base64_encode(serialize($data));
}

// --------------------------------------------------------------------

/**
 * Unserialize
 *
 * This function unserializes a data string. I first base64_decodes
 * the data from memcached storage.
 */
function _unserialize($data) {
    return unserialize(base64_decode($data));
}
票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31083175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档