Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Fuel\Core\PhpErrorException [ Notice ]: unserialize(): Error at offset 5 of 60 bytes
  • Hello,
    This line is giving an error.

    <?php $profile unserialize($item->profile_fields);?>

    profile_fields value is 
    a:1:{s:8:"fullname";s:11:"Frank Rocco";}
  • What does a var_dump($item->profile_fields); show you?

    If you're using Auth, chances are it is already unserialized. The value itself is perfectly fine.
  • Hi,

    var_dump

    string(60) "a:1:{s:8:"fullname";s:11:"Frank Rocco";}"
  • Did you var_dump() that in exactly the same place that produces the error?
  • Attempting to loop through users and print profile data.
  • [code]
    <?php foreach ($users as $item): ?> <tr>
    <?php $profile = $item->profile_fields;?>
    <?php echo $profile['fullname'];die();?>
    [/code]
  • What is $item? ORM objects?, And where is this, in a view?
  • If that echo prints "a:1:{s:8:"fullname";s:11:"Frank Rocco";}", then I don't see why you can not unserialize it, that works fine here:

    Interactive shell

    php > $s = 'a:1:{s:8:"fullname";s:11:"Frank Rocco";}';
    php > $s = unserialize($s);
    php > var_dump($s);
    array(1) {
      'fullname' =>
      string(11) "Frank Rocco"
    }
    php >

  • $item is a ORM from user.php
    $query = Model_User::query();
    $this->template->content = View::forge('admin/user/index', $data);
  • also I am on 1.8
  • Also,

    your error message says: unserialize(): Error at offset 5 of 60 bytes

    but the example you give is only 40 bytes. So there is something very wrong here...
  • <?php foreach ($users as $item): ?>
    24    <?php $profile $item->profile_fields;?>
    25    <?php $s unserialize($profile); ?>
    26    <?php echo $s['fullname'];die();?>

    Fuel\Core\PhpErrorException [ Notice ]:
    unserialize(): Error at offset 5 of 60 bytes
  • add a

    var_dump($profile);

    on the line before you unserialize, so you can see exactly on which string it fails.
  • try {
    $profile = $item->profile_fields;
    $s = unserialize($profile);
    echo $s['fullname'];die();
    } catch(Exception $e) {
    echo $profile;
    echo $e->getMessage();die();
    }
  • I get this from var_dump

    string(60) "a:1:{s:8:"fullname";s:11:"Frank Rocco";}"
  • I created the users from

    $name = $row->first_name.' '.$row->last_name;
    Auth::create_user(
       $row->username,
       'test',
       $row->email,
       1,
       array(
           'fullname' => $name,
           'price_level' => $row->price_level,
           'markup' => $row->markup,
           'company' =>$row->company,
       )
    );
  • Hi Harro,

    I had to add FALSE in my users controller

    $this->template->content = View::forge('admin/user/index', $data, FALSE);
  • Ah, you were doing it in a view.

    Don't. First rule of MVC: logic belongs in models and (a bit less) in contollers. Views should be used for display, and should not contain any logic.
  • How would I build this in my controller to pass to the view?
    The query returns the profile_fields in a format not usable in the view.

    Thanks
  • If you use the Auth package's user model, it will happen automatically.

    If you use your own model, use the Typing observer, and define the profile_fields' data_type as "serialize". See https://github.com/fuel/auth/blob/1.9/develop/classes/model/auth/user.php#L72

    The Typing observer will take care of unpacking the data when you load, and packing it when you save, completely transparent.

    Another option is use your code in the controller or the model: loop over the result, and unserialize the fields manually.

    You should try to avoid passing any data to a view without escaping it, it's Fuel's main defence against hacking.
  • I am using SimpleAuth  and Model_User
    Is this the same as Auth?
  • Auth is the Auth package (in fuel/packages/auth). That also has models, but for Ormauth.

    However, it's perfectly fine to look at the model (see link above) to see how the typing observer is implemented, and how the properties array should look like in order to use it. The principle is the same.
  • Sorry, I am confused.
    class Model_User extends \Orm\Model

    ...
    $query = Model_User::query();
    $data['users'] = $query->get();
    $this->template->title = "Users";
    $this->template->content = View::forge('admin/user/index', $data, FALSE);

    echo $item->profile_fields['price_level']; fails
  • Do you have an example of how to do this?

    Thanks
  • I've posted that a few messages back: https://github.com/fuel/auth/blob/1.9/develop/classes/model/auth/user.php

    If you use Simpleauth and have run the auth migrations to create the user table in the correct format, you can just use this model:

    class Model_User extends \Model\Auth_User;

    instead of your current app model. Your code should still work transparently after this change, but profile_fields will now be an array instead of a serialized string.

    This assumes you always_load the Auth package in your config.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion