Welcome Guest, Not a member yet? Register   Sign In
Session creates new session id on every page load
#26

[eluser]Abdul Jamal[/eluser]
Please save this file system\application\libraries named "Session.php" now check

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


class CI_Session {

    var $flash_key = 'flash'; // prefix for "flash" variables (eg. flash:new:message)

    function CI_Session()
    {
        $this->object =& get_instance();
        log_message('debug', "Native_session Class Initialized");
        $this->_sess_run();
    }


    function regenerate_id()
    {
        // copy old session data, including its id
        $old_session_id = session_id();
        $old_session_data = $_SESSION;

        // regenerate session id and store it
        session_regenerate_id();
        $new_session_id = session_id();

        // switch to the old session and destroy its storage
        session_id($old_session_id);
        session_destroy();

        // switch back to the new session id and send the cookie
        session_id($new_session_id);
        session_start();

        // restore the old session data into the new session
        $_SESSION = $old_session_data;

        // update the session creation time
        $_SESSION['regenerated'] = time();

        // session_write_close() patch based on this thread
        // http://www.ellislab.com/forums/viewthread/1624/
        // there is a question mark ?? as to side affects

        // end the current session and store session data.
        session_write_close();
    }


    function destroy()
    {
        unset($_SESSION);
        if ( isset( $_COOKIE[session_name()] ) )
        {
            setcookie(session_name(), '', time()-42000, '/');
        }
        session_destroy();
    }

  
    function userdata($item)
    {
        if($item == 'session_id'){ //added for backward-compatibility
            return session_id();
        }else{
            return ( ! isset($_SESSION[$item])) ? false : $_SESSION[$item];
        }
    }


    function set_userdata($newdata = array(), $newval = '')
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => $newval);
        }

        if (count($newdata) > 0)
        {
            foreach ($newdata as $key => $val)
            {
                $_SESSION[$key] = $val;
            }
        }
    }


    function unset_userdata($newdata = array())
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => '');
        }

        if (count($newdata) > 0)
        {
            foreach ($newdata as $key => $val)
            {
                unset($_SESSION[$key]);
            }
        }
    }


    function _sess_run()
    {
        session_start();

        $session_id_ttl = $this->object->config->item('sess_expiration');

        if (is_numeric($session_id_ttl))
        {
            if ($session_id_ttl > 0)
            {
                $this->session_id_ttl = $this->object->config->item('sess_expiration');
            }
            else
            {
                $this->session_id_ttl = (60*60*24*365*2);
            }
        }

        // check if session id needs regeneration
        if ( $this->_session_id_expired() )
        {
            // regenerate session id (session data stays the
            // same, but old session storage is destroyed)
            $this->regenerate_id();
        }

        // delete old flashdata (from last request)
        $this->_flashdata_sweep();

        // mark all new flashdata as old (data will be deleted before next request)
        $this->_flashdata_mark();
    }


    function _session_id_expired()
    {
        if ( !isset( $_SESSION['regenerated'] ) )
        {
            $_SESSION['regenerated'] = time();
            return false;
        }

        $expiry_time = time() - $this->session_id_ttl;

        if ( $_SESSION['regenerated'] <=  $expiry_time )
        {
            return true;
        }

        return false;
    }


    function set_flashdata($key, $value)
    {
        $flash_key = $this->flash_key.':new:'.$key;
        $this->set_userdata($flash_key, $value);
    }


    function keep_flashdata($key)
    {
        $old_flash_key = $this->flash_key.':old:'.$key;
        $value = $this->userdata($old_flash_key);

        $new_flash_key = $this->flash_key.':new:'.$key;
        $this->set_userdata($new_flash_key, $value);
    }

    function flashdata($key)
    {
        $flash_key = $this->flash_key.':old:'.$key;
        return $this->userdata($flash_key);
    }


    function _flashdata_mark()
    {
        foreach ($_SESSION as $name => $value)
        {
            $parts = explode(':new:', $name);
            if (is_array($parts) && count($parts) == 2)
            {
                $new_name = $this->flash_key.':old:'.$parts[1];
                $this->set_userdata($new_name, $value);
                $this->unset_userdata($name);
            }
        }
    }

    
    function _flashdata_sweep()
    {
        foreach ($_SESSION as $name => $value)
        {
            $parts = explode(':old:', $name);
            if (is_array($parts) && count($parts) == 2 && $parts[0] == $this->flash_key)
            {
                $this->unset_userdata($name);
            }
        }
    }
}
?&gt;


Messages In This Thread
Session creates new session id on every page load - by El Forum - 07-17-2010, 01:32 PM
Session creates new session id on every page load - by El Forum - 07-18-2010, 12:55 AM
Session creates new session id on every page load - by El Forum - 07-18-2010, 11:58 AM
Session creates new session id on every page load - by El Forum - 07-18-2010, 12:23 PM
Session creates new session id on every page load - by El Forum - 07-18-2010, 12:25 PM
Session creates new session id on every page load - by El Forum - 07-18-2010, 12:51 PM
Session creates new session id on every page load - by El Forum - 07-18-2010, 03:18 PM
Session creates new session id on every page load - by El Forum - 07-18-2010, 03:23 PM
Session creates new session id on every page load - by El Forum - 07-25-2010, 07:20 PM
Session creates new session id on every page load - by El Forum - 07-25-2010, 08:18 PM
Session creates new session id on every page load - by El Forum - 07-25-2010, 08:53 PM
Session creates new session id on every page load - by El Forum - 07-25-2010, 09:31 PM
Session creates new session id on every page load - by El Forum - 07-26-2010, 05:51 AM
Session creates new session id on every page load - by El Forum - 07-26-2010, 12:22 PM
Session creates new session id on every page load - by El Forum - 07-27-2010, 08:54 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 09:09 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 09:16 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 09:26 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 09:54 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 10:35 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 11:08 AM
Session creates new session id on every page load - by El Forum - 07-27-2010, 02:43 PM
Session creates new session id on every page load - by El Forum - 07-27-2010, 02:47 PM
Session creates new session id on every page load - by El Forum - 07-28-2010, 04:57 PM
Session creates new session id on every page load - by El Forum - 07-28-2010, 05:01 PM
Session creates new session id on every page load - by El Forum - 07-29-2010, 12:41 AM
Session creates new session id on every page load - by El Forum - 10-13-2010, 04:19 PM
Session creates new session id on every page load - by El Forum - 10-13-2010, 04:37 PM
Session creates new session id on every page load - by El Forum - 10-14-2010, 12:35 AM
Session creates new session id on every page load - by El Forum - 10-14-2010, 02:30 AM
Session creates new session id on every page load - by El Forum - 10-14-2010, 02:40 AM
Session creates new session id on every page load - by El Forum - 10-14-2010, 06:41 AM
Session creates new session id on every page load - by El Forum - 10-14-2010, 07:22 AM
Session creates new session id on every page load - by El Forum - 10-14-2010, 07:45 AM
Session creates new session id on every page load - by El Forum - 10-14-2010, 08:05 AM
Session creates new session id on every page load - by El Forum - 05-26-2011, 05:51 PM
Session creates new session id on every page load - by El Forum - 08-09-2011, 11:37 AM
Session creates new session id on every page load - by El Forum - 08-11-2011, 06:40 PM
Session creates new session id on every page load - by El Forum - 08-13-2011, 08:08 PM
Session creates new session id on every page load - by El Forum - 08-15-2011, 03:02 AM
Session creates new session id on every page load - by El Forum - 02-03-2012, 12:21 PM
Session creates new session id on every page load - by El Forum - 02-03-2012, 05:32 PM
Session creates new session id on every page load - by El Forum - 02-03-2012, 05:40 PM
Session creates new session id on every page load - by El Forum - 02-16-2012, 01:47 PM
Session creates new session id on every page load - by El Forum - 02-24-2012, 10:32 AM
Session creates new session id on every page load - by El Forum - 03-16-2012, 06:57 AM
Session creates new session id on every page load - by El Forum - 04-11-2012, 02:49 PM
Session creates new session id on every page load - by El Forum - 06-05-2012, 04:45 PM
Session creates new session id on every page load - by El Forum - 06-05-2012, 05:14 PM
Session creates new session id on every page load - by El Forum - 06-05-2012, 07:31 PM
Session creates new session id on every page load - by El Forum - 06-06-2012, 11:26 AM
Session creates new session id on every page load - by El Forum - 06-06-2012, 11:38 AM
Session creates new session id on every page load - by El Forum - 06-06-2012, 11:48 AM
Session creates new session id on every page load - by El Forum - 06-10-2012, 11:43 AM
Session creates new session id on every page load - by El Forum - 06-22-2012, 01:26 AM
Session creates new session id on every page load - by El Forum - 06-22-2012, 02:00 AM
Session creates new session id on every page load - by El Forum - 06-22-2012, 03:58 AM
Session creates new session id on every page load - by El Forum - 07-06-2012, 10:30 AM
Session creates new session id on every page load - by El Forum - 10-19-2012, 12:23 PM
Session creates new session id on every page load - by El Forum - 11-23-2012, 05:01 AM
Session creates new session id on every page load - by El Forum - 04-03-2013, 02:30 PM
Session creates new session id on every page load - by El Forum - 04-04-2013, 01:36 PM
Session creates new session id on every page load - by El Forum - 04-22-2013, 11:16 AM
Session creates new session id on every page load - by El Forum - 04-22-2013, 12:40 PM
Session creates new session id on every page load - by El Forum - 04-22-2013, 01:52 PM
Session creates new session id on every page load - by El Forum - 04-23-2013, 05:25 AM
Session creates new session id on every page load - by El Forum - 08-21-2013, 11:20 AM
Session creates new session id on every page load - by El Forum - 08-22-2013, 12:05 AM
Session creates new session id on every page load - by El Forum - 08-25-2013, 03:40 AM
Session creates new session id on every page load - by El Forum - 12-18-2013, 07:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB