dsfs64664 2013-09-23 19:21
浏览 72
已采纳

函数反序列化偏移错误

I'm building a multilanguage website with Laravel4.

In the database i have column named "content" that contains serialized values for multiple languages. For example:

a:3:{s:2:"gb";s:15:"This is English";s:2:"es";s:5:"Hola!";s:2:"si";s:19:"Slovenija je zakon!";}

The serialized array contains of:

  1. Language abbreviation, taken from Session
  2. Content that comes from the input field

Now when I add new language to the database it creates new serialized string. Great! But when I want to unserialize that string and add a value into it, i get the following error:

unserialize() [<a href='function.unserialize'>function.unserialize</a>]: Error at offset 0 of 30 bytes

Any ideas what is going on? I understand the meaning of the error, but it just makes no sense, since I'm sure that value in the database is serialized string.

public function setContentAttribute($value)
{   
    $lang = (Session::has('my.locale') ? Session::get('my.locale') : Config::get('app.locale'));
    /* Create new serialized string */
    if(empty($this->content)) {    
        $data[$lang] = $value['content'];
        $this->attributes['content'] = serialize($data);
    /* Update values */
    } else {
        $data = $this->content;
        $data = unserialize($data)
        $data[$lang] = $value['content'];
        $this->attributes['content'] = serialize($data);
    }
}

P.S: I'm using mutators for adding values to database.

I hope it's clear enough. If there is anything unclear, please comment and I'll fix it.
Thanks!

  • 写回答

1条回答 默认 最新

  • duaeim2874 2013-09-23 20:43
    关注

    Ok, I've managed to fix it. I was unserializing my code twice - once in the accessor and once in the mutator. Here is a working example:

    public function getVsebinaAttribute($value)
        {
            $data = unserialize($value);
            $lang = $this->getLang();
            if (!empty($data[$lang])) {
                return $data[$lang];
            } else {
                return '# Value has not yet been added';
            }
        }
        public function setVsebinaAttribute($value)
        {
            if (isset($this->attributes['vsebina'])) {
                $data = unserialize($this->attributes['vsebina']);
            } else {
                $data = array();
            }
    
            $lang = $this->getLang();
            $data[$lang] = $value;
            $this->attributes['vsebina'] = serialize($data);
        }
    
        protected function getLang()
        {
            return Session::has('my.locale') ? Session::get('my.locale') : Config::get('app.locale');
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面