How to Redirect Users Back to Their Previous Page After Successful Login?

Barbara Streisand
Release: 2024-11-01 04:45:02
Original
827 people have browsed it

How to Redirect Users Back to Their Previous Page After Successful Login?

Redirecting to the Previous Page After Successful Login

When a user logs in to a website, it's often desirable to redirect them back to the page they were previously browsing. This provides a seamless user experience and eliminates the need for manual navigation after login.

To achieve this goal, a common approach is to pass the user's current page URL as a $_GET variable to the login form. For instance, if a user is on the article page "comment.php?articleid=17" when they attempt to leave a comment, the URL would include the current location.

The login page (login.php) should then check for the presence of $_GET['location']. If it exists, the page can redirect the user to that specific URL after successful login. Here's how you can implement it:

In login.php:

<code class="php">echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
    echo htmlspecialchars($_GET['location']);
}
echo '" />';</code>
Copy after login

This hidden input field stores the current page URL and sends it along with the login form submission.

In login-check.php:

<code class="php">session_start();

$redirect = NULL;
if(isset($_POST['location']) && $_POST['location'] != '') {
    $redirect = $_POST['location'];
}

if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
    $url = 'login.php?p=1';
    if(isset($redirect)) {
        $url .= '&amp;location=' . urlencode($redirect);
    }
   header("Location: " . $url);
   exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
    $url = 'login.php?p=2';
    if(isset($redirect)) {
        $url .= '&amp;location=' . urlencode($redirect);
    }
   header("Location:" . $url);
   exit();
}
elseif(isset($_SESSION['id_login'])) {
    if($redirect) {
        header("Location:". $redirect);
    } else {
        header("Location:login.php?p=3");
    }
    exit();
}</code>
Copy after login

In this login-check.php script:

  1. It checks for the $_POST['location'] variable and assigns it to $redirect if it exists.
  2. The script handles login errors and redirects to the login page with the appropriate error parameter.
  3. Upon successful login, it checks the redirect URL and redirects the user to that page if present. Otherwise, it redirects to login.php with a successful login message.

This mechanism allows for smooth redirection to the page the user was browsing before login, enhancing the user experience and simplifying the login process.

The above is the detailed content of How to Redirect Users Back to Their Previous Page After Successful Login?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template