Welcome Guest, Not a member yet? Register   Sign In
[Code snippet] Redirect to last page
#1

[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.
#2

[eluser]tryacc[/eluser]
How about if the user refreshes page :-)

We may think of making another flashdata for the page before the previous...but this would go as a loop, one before the previous, one before the previous before the previous... :-P{

[I think I started this topic on wrong forum...]




Theme © iAndrew 2016 - Forum software by © MyBB