CodeIgniter Forums
My_Session library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: My_Session library (/showthread.php?tid=44965)



My_Session library - El Forum - 09-02-2011

[eluser]mortenfrisi[/eluser]
Hi,

I want to give all my visitors an id when they enter the site (to track them before they sign up). I have made this new My_Session:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Session extends CI_Session {

    /**
     * Create a new session
     *
     * @access    public
     * @return    void
     */
    function sess_create()
    {
        $sessid = '';
        while (strlen($sessid) < 32)
        {
            $sessid .= mt_rand(0, mt_getrandmax());
        }

        // To make the session ID even more secure we'll combine it with the user's IP
        $sessid .= $this->CI->input->ip_address();

        //my edit
        $hashed_sessid = md5(uniqid($sessid, TRUE));


        $this->userdata = array(
                            'session_id'    => $hashed_sessid,
                            'ip_address'    => $this->CI->input->ip_address(),
                            'user_agent'    => substr($this->CI->input->user_agent(), 0, 50),
                            'last_activity'    => $this->now,
                            //my edit
                            'user_data'     => $this->_serialize(array('user_sess_id' => $hashed_sessid))
                            );

        // Save the data to the DB if needed
        if ($this->sess_use_database === TRUE)
        {
            $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
        }

        // Write the cookie
        $this->_set_cookie();
    }


}

When a user enters the site it creates this in the user_data (in the ci_session table):
Code:
a:1:{s:12:"user_sess_id";s:32:"67c38264fefa5663ce12e1b0e8ad4c57";}
Perfect!

But when I add more data to the session, e.g. when I log in, it adds the data in a wrong way, like it seems creates a "double post":
Code:
a:6:{s:9:"user_data";s:66:"a:1:{s:12:"user_sess_id";s:32:"67c38264fefa5663ce12e1b0e8ad4c57";}";s:12:"user_sess_id";s:32:"67c38264fefa5663ce12e1b0e8ad4c57";s:7:"user_id";s:32:"f300327e9a9799167968f32047a18055";s:5:"email";s:28:"[email protected]";s:8:"usertype";s:5:"agent";s:6:"status";s:1:"1";}

I have tried to change the serialize/unserialize function, but no luck. Anyone who can help?