How to get two previous page URLs in php

I have a page called Login.php . I need the url of the page to the last page.

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

The problem is that I want the user to be redirected to the second to the 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 the 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 and last URL, not the last URL. How to do it in PHP?

+5
source share
3 answers

Do what other people have done:

First decision:

Create a hidden field on your pages so that when a user login.php on login he will be moved to the login.php page with the previous link. Then you can do whatever you want (for example, save it to $_SESSION['url'] ) using this link.

The second solution:

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

NOTE. Always remember to sanitize user data.

+4
source

But I want to know if there is a way to get the two previous URLs directly?

Yes, you need a session. eg

 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) that linked to the user agent on the current page. This is set by the user agent. Not all user agents will install this, but some provide the ability to modify HTTP_REFERER as a function. In short, this cannot be trusted.

+1
source

I believe the @GinoDeMaria answer works, but it does some unnecessary checks and still makes some calls to assign variables.

USING SESSIONS

Thus, an easier way to do this is:

 $_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 that Gino has said that the user agent does not always set $_SERVER['HTTP_REFERER'] correctly.

USE OF PROHIBITED VALUES IN THE APPLICATION

This method is a little more elegant and can be much more useful. Just tell check_login.php where to redirect the user after logging in.

First, we will indicate the login page to which we need to redirect. Therefore, instead of going to login.php , go to login.php?redirect=http%3A%2F%2Fexample.com%2Fpost%2F1 . (You may notice that the redirect URL looks strange, this is due to the URL encoding. Either use such a tool or check the PHP urlencode () function ).

Now in your login.php you should add a hidden field that contains this value:

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

Then in check_login.php just get this value:

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

And there you go.

+1
source

All Articles