NaszvadiG
4/17/2014 - 8:58 AM

Session.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Session Config
| -------------------------------------------------------------------------
 */

/**
 * Set namespacing for specific application in case
 * user is using multiple applications run off of the same server.
 *
 * @var string
 **/

$config['app_name'] = '';
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter Native Session Library
 *
 * @package     Session
 * @subpackage  Libraries
 * @category    Session
 * @author      Topic Deisgn
 */

class MY_Session
{
    protected $app_name = '';
    protected $store = array(); 
    
    // --------------------------------------------------------------------

    /**
     * Constructor
     *
     * @access  public
     * @param   array   config preferences
     *
     * @return  void
     **/

    function __construct($config = array())
    {
        if ( ! isset($_SESSION)) 
        {
            session_start();
        }
        $this->initialize($config);
    }
    
    // --------------------------------------------------------------------

    /**
     * Initialize the configuration options
     *
     * @access  private
     * @param   array   config options 
     * @return  void
     */    
     private function initialize($config)
     {    
        foreach ($config as $key => $val)
        {
            if (method_exists($this, 'set_'.$key))
            {
                $this->{'set_'.$key}($val);
            }
            else if (isset($this->$key))
            {
                $this->$key = $val;
            }
        }
        if(isset($_SESSION[$this->app_name]) )
        {
            $this->store = $_SESSION[$this->app_name];
            if(! $this->is_expired())
            {
                return;
            }
        }
        $this->create_session();
    }
    /**
     * Create Session
     *
     * @access  public
     * @return  void
     */
    public function create_session()
    {
        $ci = get_instance();
        $_SESSION[$this->app_name] = array(
            'session_id' => md5(microtime()),
            'expire_at' => time() + $ci->config->item('sess_expiration')
        );
        $this->store = $_SESSION[$this->app_name];
    }
    /**
     * Check if session is expired
     *
     * @access  public
     * @return  void
     */
    public function is_expired()
    {
        if ( ! isset($this->store['expire_at'])) 
        {
            return TRUE;
        }
        return (time() > $this->store['expire_at']);
    }
    /**
     * Destroy session
     *
     * @access  public
     */
    public function sess_destroy()
    {
        $this->create_session();
    }
    /**
     * Get specific user data element
     *
     * @access  public
     * @param   string  element key
     * @return  object  element value
     */
    public function userdata($value)
    {
        if ($value = 'session_id') 
        {
            return $this->store['session_id'];
        }
        if (isset($this->store[$value])) 
        {
            return $this->store[$value];
        }
        else
        {
            return FALSE;
        }
    }
    /**
     * Set value for specific user data element
     *
     * @access  public
     * @param   array  list of data to be stored
     * @param   object  value to be stored if only one element is passed
     * @return  void
     */
    public function set_userdata($data = array(), $value = '')
    {
        if(is_string($data))
        {
            $data = array($data => $value);
        }
        foreach ($data as $key => $val) 
        {
            $this->store[$key] = $val;
        }
        $_SESSION[$this->app_name] = $this->store;
    }
}