-
Notifications
You must be signed in to change notification settings - Fork 7.6k
PHPSession
(Originally discussed at http://codeigniter.com/forums/viewthread/44945/)
This is a session library that uses native PHP sessions. It stores data server-side, instead of client-side (like regular CI sessions).
This library also supports 'flash' variables, as described in this forum thread. These let you set a variable that will only exist for one page load, then be automatically destroyed (unless you specify you want to keep them for one more load). Useful for passing variables from one page to the next (ie, error messages) without having to do anything in the URL.
application/init/init_phpsession.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
if ( ! class_exists('PhpSession'))
{
require_once(APPPATH.'libraries/phpsession'.EXT);
}
$obj =& get_instance();
$obj->phpsession = new PhpSession();
$obj->ci_is_loaded[] = 'phpsession';
?>
application/libraries/phpsession.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Phpsession {
var $_flash = array();
// constructor
function Phpsession() {
session_start();
$this->flashinit();
}
/* Save a session variable.
* @paramstringName of variable to save
* @parammixedValue to save
* @paramstring (optional) Namespace to use. Defaults to 'default'. 'flash' is reserved.
*/
function save($var, $val, $namespace = 'default') {
if ($var == null) {
$_SESSION[$namespace] = $val;
} else {
$_SESSION[$namespace][$var] = $val;
}
}
/* Get the value of a session variabe
* @paramstring Name of variable to load. null loads all variables in namespace (associative array)
* @paramstring(optional) Namespace to use, defaults to 'default'
*/
function get($var = null, $namespace = 'default') {
if(isset($var))
return isset($_SESSION[$namespace][$var]) ? $_SESSION[$namespace][$var] : null;
else
return isset($_SESSION[$namespace]) ? $_SESSION[$namespace] : null;
}
/* Clears all variables in a namespace
*/
function clear($var = null, $namespace = 'default') {
if(isset($var) && ($var !== null))
unset($_SESSION[$namespace][$var]);
else
unset($_SESSION[$namespace]);
}
/* Initializes the flash variable routines
*/
function flashinit() {
$this->_flash = $this->get(null, 'flash');
$this->clear(null, 'flash');
}
/* Saves a flash variable. These are only saved for one page load
* @paramstringVariable name to save
* @parammixedValue to save
*/
function flashsave($var, $val) {
$this->save($var, $val, 'flash');
}
/* Gets the value of a flash variable. These are only saved for one page load, so the variable must
* have either been set or had flashkeep() called on the previous page load
* @paramstringVariable name to get
*/
function flashget($var) {
if (isset($this->_flash[$var])) {
return $this->_flash[$var];
} else {
return null;
}
}
/* Keeps the value of a flash variable for another page load.
* @paramstring(optional) Variable name to keep, or null to keep all. Defaults to keep all (null)
*/
function flashkeep($var = null) {
if ($var != null) {
$this->flashsave($var, $this->flashget($var));
} else {
$this->save(null, $this->_flash, 'flash');
}
}
}
?>
Like any CI library, you can either autoload it:
configs/autoload.php
For CI 1.4.x
$autoload['core'] = array('phpsession');
For CI 1.5.x because $autoload['core'] is deprecated in this version
$autoload['libraries'] = array('phpsession');
Or, load it manually inside any controller method:
$this->load->library('phpsession');
function mymethod() {
$this->load->library('phpsession');
// save a variable to the session
$this->phpsession->save('foo','bar');
// save a variable in a namespace (other than default)
$this->phpsession->save('foo','bar','mynamespace');
// read a var from the session
$foo = $this->phpsession->get('foo');
// get var from a namespace
$foo = $this->phpsession->get('foo','mynamespace');
// get all session vars (from default namespace)
$all = $this->phpsession->get();
// get all session vars from given namespace
$all = $this->phpsession->get(null,'mynamespace');
// clear session var
$this->phpsession->clear('foo');
// clear all vars (default namespace)
$this->phpsession->clear();
// clear all vars in given namespace
$this->phpsession->clear(null, 'mynamespace');
// rest of your method
$this->load->view('myview');
}
When accessing a page that requires a login, redirect to a login page, and then upon successful login, return to the original page.
At the top of any page that requires a login (or in the constructor of the controller, if the whole thing requires login), I have:
$this->phpsession->flashsave('returnurl', $this->uri->uri_string() );
redirect('user/login');
User::login() is my login controller, and the relevant part of the code to make use of the flash variables is:
if ($login_successful) {
if ($url = $this->phpsession->flashget('returnurl') ) {
redirect($url);
} else {
redirect('');
}
} else {
$this->phpsession->flashkeep('returnurl');
}
This makes it so when they go to a password-required page, eg, "foo/bar", they are redirected to the login page. Upon successful login, they are redirected back to "foo/bar", which makes for a nice user experience - eg, they can bookmark that page, and even if they need to login, they don't have to navigate there again.