Sometimes I want to be able to send users “back where you came from.” I use this most often in my security library where the user tries to go to a page that requires authentication, they are sent to the login page, maybe they forgot their password so they get it mailed to them, then they successfully login. I wanted to make the site take them back to the page they originally wanted to load. Another time I use this feature is when the user if filling in a form. If the form has validation errors, then the form is reloaded, if they press Cancel, then I want to take them back where they were before they started filling in the form (not the previous page which was the same form). Many of my forms can be reached in more than one way.
This is why I wrote the Whence Library. It simply tracks where the user has been. You could easily make breadcrumbs from the data. The data is stored in a session variable array of configurable size (default = 10). To add a breadcrumb to the current page, call
$this->whence->push()
or
$this->whence->push(arbitrary_query_string_to_pass_to_redirect)
Going back (similar to clicking the back button… but under server control) is simple too…
$this->whence->popwhence()
or
$this->whence->popwhence(number_of_breadcrumbs_to_pop)
You can also arbitrarily remove breadcrumbs using $this->whence->pop(number_of_breadcrumbs_to_pop) but that hardly ever comes up.
config/whence.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$config['maxwhence']= 10;
$config['homepage']= 'home/index';
// don't forget to autoload sessions
?>
libraries/whence.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Whence
{
protected $maxwhence=10;
protected $homepage='';
protected $_ci=null;
function __construct($config)
{
$this->_ci =& get_instance();
// there will always be exactly maxwhence elements in the array
$this->maxwhence = $config['maxwhence'];
$this->homepage = $config['homepage'];
if(!isset($this->_ci->session->userdata['whence']))
{
$this->clearwhence();
}
}
function Whence($config)
{
$this->__construct($config);
}
function clearwhence()
{
$defaultwhence = strtolower($this->homepage);
$whencearray = array();
for($i=0;$i<$this->maxwhence;$i++)
{
$whencearray[]=$defaultwhence;
}
$this->_ci->session->set_userdata(array("whence"=>$whencearray));
}
function isajax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest");
}
function push($uri='')
{
if($uri=='')
{
$uri =strtolower(substr(stristr($_SERVER['REQUEST_URI'],'index.php?/'),11));
if($uri=='')
{
$uri = strtolower(substr(stristr($_SERVER['REQUEST_URI'],'index.php/'),10));
if($uri=='')
{
$uri = $this->homepage;
}
}
}
if($this->isajax() || $uri==$this->_ci->session->userdata['whence'][$this->maxwhence-1] )
{
// ajax requests and page reloads do not count
return;
}
// shift
$whencearray = $this->_ci->session->userdata['whence'];
array_shift($whencearray);
array_push($whencearray,$uri);
$this->_ci->session->set_userdata('whence',$whencearray);
}
function popwhence($n=1)
{
if(!isset($this->_ci->session->userdata['whence']))
{
// if the session timed out then start over
$this->clearwhence();
}
$n++; // last element is where we are NOW,not where we came from
$whencearray = $this->_ci->session->userdata['whence'];
$defaulturi = $this->homepage;
$whence=$defaulturi;
for($j=0;$j<$n;$j++)
{
// shift the array
$whence=array_pop($whencearray);
array_unshift($whencearray,$defaulturi);
}
$this->_ci->session->set_userdata('whence',$whencearray);
redirect($whence);
}
function pop($n=1)
{
if(!isset($this->_ci->session->userdata['whence']))
{
// if the session timed out then start over
$this->clearwhence();
}
$n++; // last element is where are NOW,not where we came from
$whencearray = $this->_ci->session->userdata['whence'];
$defaulturi = $this->homepage;
$whence=$defaulturi;
for($j=0;$j<$n;$j++)
{
// shift the array
$whence=array_pop($whencearray);
array_unshift($whencearray,$defaulturi);
}
$this->_ci->session->set_userdata('whence',$whencearray);
}
function dump()
{
print_r($this->_ci->session->userdata['whence']);
}
}
?>
