ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Native PHP Session Library

October 07, 2008 6:02pm

Subscribe [7]
  • #1 / Oct 07, 2008 6:02pm

    Adam Griffiths

    316 posts

    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

    <?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 / Oct 09, 2008 9:51am

    steelaz

    252 posts

    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 / Oct 17, 2008 6:08am

    yaron

    2 posts

    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.

    <?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 / Jul 10, 2012 6:19am

    Adeel Ahmad

    1 posts

    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 / Jul 19, 2012 3:58pm

    ivan dan

    1 posts

    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 😊

  • #6 / Aug 27, 2012 12:59pm

    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

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases