Welcome Guest, Not a member yet? Register   Sign In
How get referring URL?
#1

[eluser]brigham[/eluser]
What's the best way to get the URL of the page "before" the current page?

E.g.,
1. User tries to go to page X, but page X requires a logged in user, so
2. Page X bounces user to the login page.
3. The login page, upon completion, sends user on to page X.

I assume that there is an elegant solution to this...
#2

[eluser]@li[/eluser]
I've wondered the same thing, and if a good solution exists for this I'd like to implement it. Any ideas?
#3

[eluser]thurting[/eluser]
$_SERVER['HTTP_REFERER'] is inconsistent so it isn't the best solution.

The best method is to store the referring URL into a session variable. Something like below would work but obviously needs to be changed to fit you specs - it is just to help you get started.

Code:
function _authorize()
{
  // maybe use $this->uri->uri_string() or $this->uri->ruri_string(n) below
  $_SESSION['referer'] = $_SERVER['PHP_SELF'];

  if (!AUTHORIZED)
  {
    REDIRECT TO LOGIN
  }
}

function index ()
{
  $this->_authorize();
}

function login ()
{
  $user = $this->user_model->authenticate();

  if ($user)
  {
    AUTHORIZE
    $url = (isset($_SESSION['referer']))? $_SESSION['referer'] : 'index';
    unset($_SESSION['referer']);
    redirect($url);
  }
}

you could probably use CI sessions but I prefer to use native sessions for something like this.
#4

[eluser]brigham[/eluser]
How about using a cookie?
#5

[eluser]thurting[/eluser]
You could use a cookie if you really want but I don't think it makes sense to do that. For this I think a session is best. The value is only needed for a very short amount of time, and also deals directly with the session at hand - i.e. the first page the user landed on during the current session. There is no need to keep this value on the users computer with a cookie.
#6

[eluser]einstein[/eluser]
or better use a variable in the config file.
Open config.php and place this line of code
$config['previousPage'] = "";
after that you set a value to this variable on page X
like this:
$this->config->set_item('previousPage', 'http://previous.url.com');
and retrive it on page y like this:
$this->config->item('previousPage');

This could be of help for global variable, i think this an easy and good sollution, and will help you avoid overloading the session.

As far as i know the session can handle only a few kb / session.

I hope this helps (sorry i forgot to look at the post's date)
Big Grin
but maybe it will help others
#7

[eluser]Rick Jolly[/eluser]
einstein, that won't work for a couple of reasons.
#8

[eluser]beemr[/eluser]
Similar discussion here redirect to current page.

After you get the referrer, you will have to compare with the current URI to prevent an Auth loop. In my scheme, if the URI's are the same I keep backing out one folder at a time until the user reaches a page for which they are authorized.
#9

[eluser]Xeoncross[/eluser]
[quote author="einstein" date="1218056774"]As far as i know the session can handle only a few kb / session.[/quote]

If you use the recommended method of a storing session data in a system file or DB - then this doesn't matter.

Also, storing something like this in a config file is a really bad idea - not only will each person that requests the page get overwrite the last value - but users will get other users redirect values. It also will take a lot of overhead to do something that could be done with sessions so easy.
#10

[eluser]brettalton[/eluser]
Sorry for upping an old post, but this is the solution I use:

Say I have a controller and the page (or second segment) determines I'm not logged in. (e.g trying to view '/pages/books' but that is for logged in members only):
Code:
function page()
{
    if ( ! LOGGED_IN)
    {
        redirect('/auth/login/page/books');
    }
}

Then in my auth controller, I read the third and fourth segment and redirect to those after I've completed my login process.

Code:
function login($seg3, $seg4)
{
    //login process
    redirect($seg3.'/'.$seg4);
}

Thus redirecting the user to '/page/books/' after logging in.

To make it automatically redirect when clicking 'login', 'logout', 'register', etc. to return the user to their original page, I build the anchor automatically with uri_string() (requires the URL helper). Since the URL could become '/auth/login/auth/login/page/books' (if someone clicked the link to 'books' a second time, after already being redirected, I remove all instances of '/auth/login' in the '/auth/login' link:
Code:
<li><a href="/auth/login&lt;?php echo str_ireplace('/auth/login', '', uri_string()); ?&gt;">Login</a></li>

Thus, for the 'register' link, it'll look like:
Code:
<li><a href="/auth/register&lt;?php echo str_ireplace('/auth/register', '', uri_string()); ?&gt;">Register</a></li>

People could hack the URI, but it'll just refer them to a non-existant page if they do. If they delete the third and fourth segment so it just says '/auth/login/', then they'll get redirect to '/', which is the home page.

Works for me.




Theme © iAndrew 2016 - Forum software by © MyBB