Shafizadeh
Shafizadeh

Reputation: 10340

How to obtain two previous page's URL in php

I have a page named Login.php. I need the URL of the page before the last page.

When the user login form will submit to check_login.php which checks their username and password. Then I will redirect the user to index.php.

The issue is I want the user to be redirected to the second to last page they visited.

EXAMPLE:

First URL: www.example.com/posts/1

Second URL: www.example.com/login.php

Third URL: wwww.example.com/check_login.php

So, if username and password are correct => header('location: www.example.com/posts/1');

There is a solution to get the previous URL:

// login.php
$_SESSION['url'] = $_SERVER['HTTP_REFERER'];

// check_login.php
header('location: '.$_SESSION['url']);

But I need to get the second to last URL, not the last URL. How can I do this in PHP?

Upvotes: 7

Views: 18242

Answers (3)

Hurricane Development
Hurricane Development

Reputation: 2464

I suppose @GinoDeMaria's answer works, but it is doing some unnecessary checks and still making multiple calls to assign variables.

USING SESSIONS

So a more simple way to do it would be:

$_SESSION['2last_url'] = isset($_SESSION['last_url']) ? $_SESSION['last_url'] : null;
$_SESSION['last_url'] = $_SERVER['HTTP_REFERER'];

So $_SESSION['2last_url'] contains the value you are looking for.

Now as Gino said the user agent does not always set $_SERVER['HTTP_REFERER'] properly.

USING REDIRECT VALUES IN REQUEST

This method is a bit more elegant, and could prove to be much more useful. Simply tell the check_login.php where to redirect the user after login.

First we tell the login page where to redirect. So instead of going to login.php, we go to login.php?redirect=http%3A%2F%2Fexample.com%2Fpost%2F1. (You may notice the redirect URL looks odd, this is due to URL encoding. Either use a tool like this or check out PHP's urlencode() function).

Now in your login.php you must add a hidden field which contains this value:

<input type="hidden" name="redirect" value="<?php echo $_GET['redirect']; ?>">

Then in check_login.php just get that value:

header("Location: " . $_POST['redirect']);

And there you go.

Upvotes: 3

Muntashir Akon
Muntashir Akon

Reputation: 9431

Do what other people have done:

First solution:

Create a hidden field in your pages so that when a user clicks on login it will take him to the login.php page with the previous link. Then you can do anything you want (like, saving it to $_SESSION['url']) with this link.

Second Solution:

Imagine you have an anchor like this: <a href="login.php">login</a>. Instead of using just login.php you can use login.php?redirect_to=http://example.com/post/1 so that you can access the url from your login.php page with a $_GET['redirect_to'] request.

NOTE: Always remember to sanitize user input data.

Upvotes: 8

Gino De Maria
Gino De Maria

Reputation: 51

But I want to know there is a way to I can obtain two previous url directly ?

Yes, you have need of the session. e.g

if(!isset($_SESSION['url']))
   $lurl = array();
else
   $lurl = $_SESSION['url']);
$count = count($lurl);
$lurl[$count % 2] = $_SERVER['HTTP_REFERER'];
$_SESSION['url'] = $lurl;

Caveat: (php manual) The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

Upvotes: 1

Related Questions