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.


Messages In This Thread
Native PHP Session Library - by El Forum - 10-07-2008, 03:02 PM
Native PHP Session Library - by El Forum - 10-09-2008, 06:51 AM
Native PHP Session Library - by El Forum - 10-17-2008, 03:08 AM
Native PHP Session Library - by El Forum - 07-10-2012, 03:19 AM
Native PHP Session Library - by El Forum - 07-19-2012, 12:58 PM
Native PHP Session Library - by El Forum - 08-27-2012, 09:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB