sharin gan
sharin gan

Reputation: 383

Store the URL of the previous page in PHP?

In my website, after a user selects login from some page, and after logging in I want to redirect him to the previous page.

If I define HTTP referer variable ($_SERVER['HTTP_REFERER']), on the login page it wont work because for form validation, it will again come back to login page and referer variable will be changed.

e.g.

page1.php -> login.php(referer=page1) -> login action validation(referer=login.php).

How to solve this?

Upvotes: 1

Views: 1378

Answers (3)

Divyesh Jesadiya
Divyesh Jesadiya

Reputation: 957

your login.php file should be like this.

if(isset($_SESSION['userName'] && $_SESSION['Password']))
{
    header('location:'. $_SERVER['HTTP_REFERER']);
}
else{
    */ your login code /*
}

Upvotes: 0

Adrian Attwood
Adrian Attwood

Reputation: 11

You can try $_SERVER['REDIRECT_URL']

$_SERVER

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98881

Use sessions

login.php

session_start();
$_SESSION["referer"] = $_GET['referer'];

page2.php

session_start();
echo $_SESSION["referer"];

Upvotes: 4

Related Questions