Welcome Guest, Not a member yet? Register   Sign In
Converting session cookie to array?
#1

[eluser]jmb727[/eluser]
Is there a way to convert the session data saved by CodeIgniter in a cookie (ci_session) to an array? I've written the following function to achieve this aim but I wondered if there was a function built into CodeIgniter that did the same thing.

Code:
public function extract_session_variables()
    {
        $session_cookie = $this->input->cookie('ci_session');
        
        if ($session_cookie == FALSE) {
            return FALSE;
        }
        
        $session_cookie = $this->encrypt->decode($session_cookie);
        
        $x01 = explode("{", $session_cookie);
        $x02 = explode("}", $x01[1]);
        
        $x03 = array_reverse(preg_split('/(s:[0-9]+:)|(i:)/', $x02[0]));
        array_pop($x03);
        $x03 = array_reverse($x03);
        
        $cookieData = array();

        foreach ($x03 as $k => $v)
        {
            if ($k % 2 == 0) {
                $name = preg_replace('/[";]/', '', $v);
                $value = preg_replace('/[";]/', '', $x03[$k+1]);
                
                $cookieData[$name] = $value;
            }
        }
        
        if (empty($cookieData)) {
            return FALSE;
        }
        
        return $cookieData;
    }
#2

[eluser]theprodigy[/eluser]
Code:
$cookie = $this->input->cookie('ci_session');
    $cookie = unserialize($cookie);
    echo '<pre>'.print_r($cookie, true).'</pre>';

What CI stores in session is nothing but a serialized array.
#3

[eluser]jmb727[/eluser]
lol I knew it. thanks.




Theme © iAndrew 2016 - Forum software by © MyBB