Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeIgniter Session Cannot Store/Retrieve Multi-dimensional Array Data #1562

Closed
erikig opened this issue Jul 2, 2012 · 4 comments
Closed

Comments

@erikig
Copy link

erikig commented Jul 2, 2012

The functions below result in data loss when handling multi-dimensional arrays
Session::_serialize()
Session::_unserialize()

$app_status['title']   = 'Message Received';
$app_status['message'] = 'The message was saved to your directory C:\Users\MyUser'
$app_status['next']    = "/";
$this->session->set_userdata('app_status',$status);

The $app_status array will not be available in the session data when the function below is called later from a different controller or url

$this->session->all_userdata();

This prevents CI's Session from being a drop-in replacement for PHP's native $_SESSION handler

@erikig
Copy link
Author

erikig commented Jul 2, 2012

For now I'm using this as a work-around courtesy of the CodeIgniter forum:
http://codeigniter.com/forums/viewthread/205035/

<?php
class MY_Session extends CI_Session
{
    function __construct($config = array())
    {
        parent::__construct($config);
    }

    /**
     * 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))
        {
            array_walk_recursive($data, function(&$item,$key){
                if (is_string($item))
                {
                    $item = str_replace('\\', '{{slash}}', $item);
                }
            });
        }
        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))
        {
            array_walk_recursive($data, function(&$item,$key){
                if (is_string($item))
                {
                    $item = str_replace('{{slash}}', '\\', $item);
                }
            });

            return $data;
        }

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

@erikig erikig closed this as completed Jul 2, 2012
@akalongman
Copy link

@EllisLab Why don't you add this fix in to current stable version?

@ckdarby
Copy link
Contributor

ckdarby commented Feb 8, 2013

@LONGMANi If you want the code to be added I suggest creating a pull request :)

@narfbg
Copy link
Contributor

narfbg commented Feb 8, 2013

A PHP 5.2-compatible version of the shown work-around already is present in the develop branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants