How To Redirect To Same Page After Login In Php

Have you ever encountered a situation where you want to redirect to the same page after logging in on a website? I’ve been there too, and in this article, I will show you how to achieve this using PHP. Redirecting to the same page after login can enhance the user experience by taking users back to where they left off, making the login process seamless and convenient.

Before we dive into the implementation, let me briefly explain how the login process works in PHP. Typically, when a user enters their credentials and clicks the login button, their information is sent to a server-side script for validation. If the credentials are valid, the user is granted access to the protected pages of the website.

Now, let’s get down to business and see how we can redirect to the same page after login. The first step is to create a login form with the necessary fields, such as username and password. Here’s a simple example:

<form action="login.php" method="POST">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Login">
</form>

Next, we need to handle the login logic in the server-side script. In our case, we’ll call it “login.php”. Here’s an example of how you can implement the login logic:

<?php
session_start(); // Start the session

// Check if the user is already logged in
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true){
header("location: ".$_SERVER['HTTP_REFERER']); // Redirect to the previous page
exit;
}

// Check if the form is submitted
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$username = $_POST['username'];
$password = $_POST['password'];

// Perform the login validation
if($username === 'your_username' && $password === 'your_password'){
$_SESSION['loggedin'] = true; // Store the login status in the session
header("location: ".$_SERVER['HTTP_REFERER']); // Redirect to the previous page
exit;
} else {
echo "Invalid username or password";
}
}
?>

In the above code, we start the session using the session_start() function. This is necessary to maintain the login status across different pages. We then check if the user is already logged in by checking the value of the $_SESSION['loggedin'] variable. If the user is logged in, we redirect them to the previous page using header("location: ".$_SERVER['HTTP_REFERER']). The $_SERVER['HTTP_REFERER'] variable contains the URL of the previous page.

If the user is not logged in, we check if the login form is submitted. If it is, we retrieve the username and password from the POST data and perform the login validation. If the credentials are valid, we set $_SESSION['loggedin'] to true and redirect the user to the previous page.

Now, you might be wondering how this magic happens. When the user clicks the login button, the form data is sent to the server-side script (login.php in our case) using the HTTP POST method. This triggers the code in login.php to execute, and the login logic is performed. Once the login logic is completed, we use the header() function to redirect the user back to the previous page using the $_SERVER['HTTP_REFERER'] variable.

Remember to replace your_username and your_password with the actual credentials you want to validate against.

Conclusion

Redirecting to the same page after login in PHP is a useful technique to improve the user experience of your website. By implementing the logic described in this article, users can seamlessly continue their browsing after logging in. Remember to handle the login validation securely and protect sensitive user data.

Feel free to modify and customize the code to fit your specific requirements. Happy coding!