creating back page links in Codeigniter

后端 未结 5 1733
眼角桃花
眼角桃花 2020-12-10 20:17

I have a page with URL http://arslan/admin/category/index/0/name/asc/10 in Codeigniter. In this URL, the uri_segment start from 0. This (0

相关标签:
5条回答
  • 2020-12-10 21:04

    The most simplest way to redirect to your previous page , try this it work for me

     redirect($this->agent->referrer());
    

    you need to import user_agent library too $this->load->library('user_agent');

    0 讨论(0)
  • 2020-12-10 21:12

    I am not sure if i understand the question correctly, if not please ignore my answer, but I think you want a link to "go back to previous page", similar to the back-button in a web browser.

    If so you could use javascript to solve this by simply using this line:

    <a href="javascript:window.history.go(-1);">Go back</a>
    
    0 讨论(0)
  • 2020-12-10 21:14

    There are two ways to solve your problem: First you could place a link that is using the javascript back-function onclick, like this ...

    <a href="javascript:history.back()">go back</a>
    

    ... or you always save the current full page url into a cookie and use that for generating the back link - a helper could look like this (not tested) ...

    /**
     * save url to cookie
     */
    if(!function_exists('urlhistory_save'))
    {
        function urlhistory_save()
        {
            $CI =& get_instance();
            $CI->load->library('session');
    
            $array = array(
                'oldUrl' = $CI->session->userdata('newurl'),
                'newurl' = $CI->uri->uri_string()
            );
            $CI->session->set_userdata($array);
        }
    }
    
    /**
     * get old url from cookie
     */
    if(!function_exists('urlhistory_get'))
    {
        function urlhistory_get()
        {
            $CI =& get_instance();
            $CI->load->library('session');
    
            return $CI->session->userdata('oldurl');
        }
    }
    

    In your controller you would use urlhistory_save() to save the current URL and in the view youd could use urlhistory_get() to retreive the old address like this:

    <a href="<?php echo base_url().urlhistory_get(); ?>go back</a>
    
    0 讨论(0)
  • 2020-12-10 21:14

    You can create a Session to go to back page as:

    $this->session->set_userdata('ses_back_jobs','controller 
    name'.array_pop(explode('controller name',$this->input->server('REQUEST_URI'),2))); //Back page
    

    Then if u want to redirect to some page use it:

    redirect($this->session->userdata('ses_back_jobs'));
    

    or use it to the anchor.

    0 讨论(0)
  • 2020-12-10 21:17

    I extend the session class by creating /application/libaries/MY_Session.php

    class MY_Session extends CI_Session {
    
        function __construct() {
            parent::__construct();
    
            $this->tracker();
        }
    
        function tracker() {
            $this->CI->load->helper('url');
    
            $tracker =& $this->userdata('_tracker');
    
            if( !IS_AJAX ) {
                $tracker[] = array(
                    'uri'   =>      $this->CI->uri->uri_string(),
                    'ruri'  =>      $this->CI->uri->ruri_string(),
                    'timestamp' =>  time()
                );
            }
    
            $this->set_userdata( '_tracker', $tracker );
        }
    
    
        function last_page( $offset = 0, $key = 'uri' ) {   
            if( !( $history = $this->userdata('_tracker') ) ) {
                return $this->config->item('base_url');
            }
    
            $history = array_reverse($history); 
    
            if( isset( $history[$offset][$key] ) ) {
                return $history[$offset][$key];
            } else {
                return $this->config->item('base_url');
            }
        }
    }
    

    And then to retrieve the URL of the last page visited you call

    $this->session->last_page();
    

    And you can increase the offset and type of information returned etc too

    $this->session->last_page(1); // page before last
    $this->session->last_page(2); // 3 pages ago
    

    The function doesn't add pages called using Ajax to the tracker but you can easily remove the if( !IS_AJAX ) bit to make it do so.

    Edit: If you run to the error Undefined constant IS_AJAX, assumed IS_AJAX add the line below to /application/config/constants.php

    define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
    
    0 讨论(0)
提交回复
热议问题