15
votes

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.

5

5 Answers

40
votes

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();
}
19
votes

How about just

redirect($_SERVER['HTTP_REFERER']);

Using php's $_SERVER global variable.

This worked for me!

5
votes

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 );
}
3
votes

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.

1
votes

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