0

I am working with a login system. Here is my code to redirect some other page after successful login. I just want to go back to the previous page instead of redirect some specific page.

Code: login.php

<?PHP
require_once("./include/membersite_config.php");

if(isset($_POST['submitted']))
{
if($fgmembersite->Login())
{
    $fgmembersite->RedirectToURL("login-home.php");
}
}

?>
sanzuu
  • 192
  • 1
  • 4
  • 14

3 Answers3

1

In the previous page
add these:

session_start();
require_once("./include/membersite_config.php");
if (!$fgmembersite->CheckLogin())
{
    $_SESSION['redirect_url'] = $_SERVER['PHP_SELF']; 
    header('Location: login.php');
    exit;
}


login page

    session_start();

    require_once("./include/membersite_config.php");

    if(isset($_POST['submitted']))
    {
        if($fgmembersite->Login())
        {
            $redirect_url = (isset($_SESSION['redirect_url'])) ? $_SESSION['redirect_url'] : '/';
            unset($_SESSION['redirect_url']);
            header("Location: $redirect_url", true, 303);
            exit;
        }
    }
DunnoHowToCode
  • 531
  • 1
  • 4
  • 14
0

You can try this approach: In your login page: $comingFrom = $_SERVER['HTTP_REFERER'];

if(isset($_POST['submitted']))
{

    //or you can store it in session also.so that you can redirect it from anywhere.
    if($fgmembersite->Login())
    {
       $fgmembersite->RedirectToURL($comingFrom);
    }
 }
Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14
0

In PHP you can get the previous page using $_SERVER variable. By this $_SERVER['HTTP_REFERER'], which contains the page URL from the current page is being called.

But sometimes there is an option in the browser which some users can disable to send this information.

So beware to use this option.

Alternate to this option is to set a value in form and send it along with other data, that is still vulnerable as this can be changed by user easily.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47