Welcome Guest, Not a member yet? Register   Sign In
CI 3 using native sessions instead of driver
#1

Would this be the best way to use the native php sessions instead of using their drivers? Since their drivers are just wrappers around session functions; I figured this would be the easiest way to do it still preserving library interface

The reason behind this is I am having issues with locking with mysql database driver and redis driver. I want to use native php sessions and swap in a redis driver at the php level.

This seems to work well. Any thoughts?

 
PHP Code:
<?php
    
class MY_Session extends CI_Session 
    
{
        public function __construct(array $params = array())
        {
            $CI =& get_instance();
                    
            
// No sessions under CLI
            if (is_cli())
            {
                log_message('debug''Session: Initialization under CLI aborted.');
                return;
            }
            elseif ((bool) ini_get('session.auto_start'))
            {
                log_message('error''Session: session.auto_start is enabled in php.ini. Aborting.');
                return;
            }
            
            
// Configuration ...
            $this->_configure($params);
            $this->_config['_sid_regexp'] = $this->_sid_regexp;
    
            
// Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
            if (isset($_COOKIE[$this->_config['cookie_name']])
                && (
                    is_string($_COOKIE[$this->_config['cookie_name']])
                    OR ! preg_match('#\A'.$this->_sid_regexp.'\z#'$_COOKIE[$this->_config['cookie_name']])
                )
            )
            {
                unset($_COOKIE[$this->_config['cookie_name']]);
            }
    
            session_start
();
    
            
// Is session ID auto-regeneration configured? (ignoring ajax requests)
            if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
                && ($regenerate_time config_item('sess_time_to_update')) > 0
            
)
            {
                if ( ! isset($_SESSION['__ci_last_regenerate']))
                {
                    $_SESSION['__ci_last_regenerate'] = time();
                }
                elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
                {
                    $this->sess_regenerate((bool) config_item('sess_regenerate_destroy'));
                }
            }
            // Another work-around ... PHP doesn't seem to send the session cookie
            // unless it is being currently created or regenerated
            elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
            {
                setcookie(
                    $this->_config['cookie_name'],
                    session_id(),
                    (empty($this->_config['cookie_lifetime']) ? time() + $this->_config['cookie_lifetime']),
                    $this->_config['cookie_path'],
                    $this->_config['cookie_domain'],
                    $this->_config['cookie_secure'],
                    TRUE
                
);
            }
            $this->_ci_init_vars();        
        }
    
Reply


Messages In This Thread
CI 3 using native sessions instead of driver - by blasto333 - 12-11-2019, 07:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB