Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to referer url in codeigniter

In messaging system of my project when you get a message from a user you a email alert saying that the another user has sent a message to view the message click here (i.e the url of message) So if the user is not logged in to system he gets redirect to login page and after login it should get back to the referer url. I have made a basecontoller in core folder and extending the CI_controller the authenticating code is as follows.

function authenticate($type = 'user')
    {
        if($type == 'user')
        {
            if($this->user_id)
            {
                // user is logged in. check for permissions now
            }
            else
            {
                // user isnt logged in. store the referrer URL in a var.
                if(isset($_SERVER['HTTP_REFERER']))
                {
                    $redirect_to = str_replace(base_url(),'',$_SERVER['HTTP_REFERER']);
                }
                else
                {
                    $redirect_to = $this->uri->uri_string();
                }            

                redirect('user/login?redirect='.$redirect_to);
                exit;
            }
        }

        if($type == 'admin')
        {
            if($this->session->userdata('admin_id') && $this->session->userdata('user_type') ==5)
            {
                // Admin is logged in
            }
            else
            {
                redirect('admin/login');
                exit;
            }
        }
    }

The referer url is "http://example.com/project/pm/view_conversation?id=11" now the problem is I am getting referer url till view_conversation and not able to get the id part.

Any suggestion ?

Thank you.

like image 559
Prathamesh mhatre Avatar asked Jul 07 '12 11:07

Prathamesh mhatre


People also ask

How do I link to another page in codeigniter?

site_url() : Returns your site URL, as specified in your config file. The index. php file (or whatever you have set as your site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.

Which function is used for page redirection in codeigniter?

Use redirect() helper function.


5 Answers

How about just

redirect($_SERVER['HTTP_REFERER']);

Using php's $_SERVER global variable.

This worked for me!

like image 26
maxxon15 Avatar answered Oct 22 '22 10:10

maxxon15


This can help:

CI 2+ https://www.codeigniter.com/userguide2/libraries/user_agent.html

CI 3+ http://www.codeigniter.com/userguide3/libraries/user_agent.html

Below solution is for Codeigniter version 3

$this->load->library('user_agent');
if ($this->agent->is_referral())
{
    echo $this->agent->referrer();
}

UPDATE: interesting and useful information on how to obtain referrer information with the same user_agent library
https://www.tutorialandexample.com/user-agent-class/

like image 142
Adrian P. Avatar answered Oct 22 '22 11:10

Adrian P.


Put that code in your Login Controler

function index() {
    $this->load->library('user_agent');  // load user agent library

    //Set session for the referrer url
    $this->session->set_userdata('referrer_url', $this->agent->referrer() );  
}

After Login Redirection Code

// user is authenticated if referrer is there
if( $this->session->userdata('referrer_url') ) {
    //Store in a variable so that can unset the session
    $redirect_back = $this->session->userdata('referrer_url');
    $this->session->unset_userdata('referrer_url');
    redirect( $redirect_back );
}
like image 40
Binayak Das Avatar answered Oct 22 '22 11:10

Binayak Das


Because you have double question mark in the url, the browser ignores the url part after the second one. Use urlencode for you redirect part, like so:

redirect('user/login?redirect='.urlencode($redirect_to));

I've tested it out and it works this way.

like image 34
machineaddict Avatar answered Oct 22 '22 10:10

machineaddict


By default CI is configured to ignore the query part of the URL (the part after the '?').

See: http://codeigniter.com/user_guide/general/urls.html

like image 1
Patrick Savalle Avatar answered Oct 22 '22 11:10

Patrick Savalle