Welcome Guest, Not a member yet? Register   Sign In
Native PHP Session Library
#1

[eluser]Adam Griffiths[/eluser]
Introducing the native PHP session library. You use it in exactly the same way as the CI library except for one small exception. When using the userdata function.

The second parameter lets you specify how to return the data. If you do not pass anything to it, it will return TRUE or FALSE depending on whether or not the session data is set. If you specify TRUE in the second parameter, it will return the data inside the session or FALSE if it isn't set.

I believe this is a more secure way to store data rather than using the native CI library as it only uses cookies, which has a few exploits.

Save as application/libraries/Session.php
Code:
<?php
session_start();

class CI_Session
{
    
    var $CI;
    var $DB;
    
    function CI_Session()
    {
        $this->CI =& get_instance();
        $this->DB = $this->CI->load->database();
    }
    
    function set_userdata($name, $value = NULL)
    {
        if( ! is_array($name))
        {
            if($value === NULL)
            {
                show_error("Second parameter for set_userdata missing.");
            } // if
            else
            {
                if(isset($_SESSION[$name]))
                {
                    unset($_SESSION[$name]);
                } // if
                $_SESSION[$name] = $value;
            } // else
        } // if
        else
        {
            foreach($name as $names => $key)
            {
                if(isset($_SESSION[$names]))
                {
                    unset($_SESSION[$names]);
                }
                $_SESSION[$names] = $key;
            } // forech
        } //  else
    } // set_userdata
    
    function userdata($item, $string = NULL)
    {
        if( ! $string === NULL)
        {
            if(!isset($_SESSION[$item]))
            {
                return FALSE;
            } // if
            else
            {
                return TRUE;
            } // else
        } // if
        else
        {
            if(!isset($_SESSION[$item]))
            {
                return FALSE;
            } // if
            else
            {
                return $_SESSION[$item];
            } // else
        } // else
    } // userdata
    
    function unset_userdata($userdata)
    {
        if(!is_array($userdata))
        {
            unset($_SESSION[$userdata]);
        } // if
        else
        {
            foreach($userdata as $item)
            {
                unset($_SESSION[$item]);
            } // foreach
        } // else
    } // unset_userdata
    
    function sess_destroy()
    {
        session_destroy();
    } // session_destroy
    
}

/* End of file Session.php */
/* Location: application/libraries */

I am 99% sure I covered everything, if something is missing or you want me to add something just add a reply.

Thanks.
#2

[eluser]steelaz[/eluser]
Thanks for this one, I was having trouble with IE rejecting CI session cookies, with this library so far everything seems to be working fine.
#3

[eluser]Unknown[/eluser]
you forgot the flashdata, but still thx a lot. I am writing the flashdata functions now, but since I don't write as nice code as you, but I'll post it here when done.

edit:
also changed some small stuff besides adding the functions
I tried to keep the same style as much as possible.

Code:
<?php
session_start();

class CI_Session
{
    
    var $CI;
    var $DB;
    var $CurTime;
    
    function CI_Session()
    {
        $this->CI =& get_instance();
        $this->DB = $this->CI->load->database();
        $this->CurTime = time();
        if (isset($_SESSION['flash_time']))
        {
            $this->LastTime = $_SESSION['flash_time'];
        }//if
        else
        {
            $this->LastTime = 0;
        }//else
        $_SESSION['flash_time'] = $this->CurTime;
    }
    
    function set_userdata($name, $value = NULL)
    {
        if( ! is_array($name))
        {
            if($value === NULL)
            {
                show_error("Second parameter for set_userdata missing.");
            } // if
            else
            {
                if(isset($_SESSION[$name]))
                {
                    unset($_SESSION[$name]);
                } // if
                $_SESSION[$name] = $value;
            } // else
        } // if
        else
        {
            foreach($name as $names => $key)
            {
                if(isset($_SESSION[$names]))
                {
                    unset($_SESSION[$names]);
                }
                $_SESSION[$names] = $key;
            } // forech
        } //  else
    } // set_userdata
    
    function userdata($item, $string = NULL)
    {
        if( ! $string === NULL)
        {
            if(!isset($_SESSION[$item]))
            {
                return FALSE;
            } // if
            else
            {
                return TRUE;
            } // else
        } // if
        else
        {
            if(!isset($_SESSION[$item]))
            {
                return FALSE;
            } // if
            else
            {    
                return $_SESSION[$item];
            } // else
        } // else
    } // userdata
    
    function unset_userdata($userdata)
    {
        if(!is_array($userdata))
        {
            unset($_SESSION[$userdata]);
        } // if
        else
        {
            foreach($userdata as $item)
            {
                unset($_SESSION[$item]);
            } // foreach
        } // else
    } // unset_userdata
    
    function sess_destroy()
    {
        session_destroy();
        session_start();
        $_SESSION['flash_time'] = $this->CurTime;
    } // session_destroy
    
    function set_flashdata($item, $value=NULL)
    {
        if (is_array($item))
        {
            foreach($item as $key=>$val)
            {
                $session = array(
                    'flash_'.$key=>array('
                        data'=>$val,
                        'set'=>$this->CurTime
                    )
                );
                $this->set_userdata($session);
                
            }//foreach
        }//if
        else
        {
            $session = array(
                'flash_'.$item=>array(
                    'data'=>$value,
                    'set'=>$this->CurTime
                )
            );
            $this->set_userdata($session);
        }//else
    }//set_flashdata
    
    function flashdata($item)
    {
        $session = $this->userdata('flash_'.$item);    
        if ($session['set']>=$this->LastTime)
        {
            return $session['data'];
        }//if
        else
        {
            return false;
        }//else
    }//flashdata
    
    function keep_flashdata($item)
    {
        if ($this->flashdata('flash_'.$item, true))
        {
            $_SESSION['flash_'.$item]['set'] = $this->CurTime;
        }//if
        
    }//keep_flashdata
    
}

/* End of file Session.php */
/* Location: application/libraries */
?>

edit: oops made a small mistake.
if ($session['set']>=$this->LastTime)
was
if ($session['set']>=0) for testing
code should be working now
#4

[eluser]Unknown[/eluser]
Guys just use your native session library with codeigniter 2.1.2. But this seems that redirect function of url helper stops working.. :-s
#5

[eluser]Unknown[/eluser]
Thank you very much for this post, i am new to codeigniter framework. And the session lost data issue is also iv'e encountered. This is very helpful . Once again, Thanks a lot Smile
#6

[eluser]gentleJuggernaut[/eluser]
Thank you.

For some reason ATT in my area does not work with the CI sessions. It returns corrupt data. Switching out the Session.php module for this native session module solved the issue. I will put a link to this thread in my original request for help in case someone else runs into this problem with the ATT network. Again thank you, this saved me days of downtime.

NSM




Theme © iAndrew 2016 - Forum software by © MyBB