[eluser]tryacc[/eluser]
Many people ask around how to redirect the user to the last page, lets say, after login. Here is how I do it [well, perhaps I just encountered very old posts on my searchs...] :
MODEL > returnurl.php
--------------------------
Code:
<?php
class Returnurl extends Model {
function Returnurl()
{
parent::Model();
$this->_prev = $this->session->flashdata("returnurl");
//we dont want AJAX requests to be saved as last page.
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$this->session->set_flashdata('returnurl', uri_string());
}
}
function index() {
}
function now() {
// we dont want logout>login>logout>login loop.
$donotreturn = array("/auth/login", "/auth/logout");
$url = $this->_prev;
if(isset($url)) {
if( !in_array($url, $donotreturn) ) {
redirect($url);
} else {
redirect("/");
}
} else {
redirect("/");
}
return true;
}
function give() {
return $this->_prev;
}
function give_title () {
$url = $this->_prev;
$titles = array(
"attachment" => "back to attachment page",
"main page" => "back to photo",
"posts" => "back to post list",
"links" => "back to links list"
);
//do something here to split "/"s in the stored returning url. then check if your page in_array( , $titles); then return the title to be put on the "back" link.
$splitted = explode("/", $url);
$page = $splitted[1];
$page_title = "";
if(in_array($page, $titles)) {
$page_title = $titles[$page];
} else {
$page_title = "back";
}
return $page_title;
}
}
CONFIG > AUTOLOAD.PHP
-----------------------------
We need to set "session" library, "url" helper and "returnurl" model to be autoloaded. So that we can save every new page request when it is loaded.
USAGE
-----------------------------
Code:
anchor($this->returnurl->give(), $this->returnurl->give_title(), array('title' => $this->returnurl->give_title(), 'class' => 'back_button') )
------------------------------------
You can add many more functions to this. This is just a quickly-written template.