How To Redirect To Another Page In Php After Login

How To Articles

Hey there! Today, I want to share with you my personal experience and knowledge on how to redirect to another page in PHP after login. This is a common scenario in web development, and knowing how to handle it properly can greatly enhance the user experience of your website.

Before we dive into the technical details, let me start by explaining why this feature is important. When users log in to a website, they expect to be directed to a specific page that is relevant to their logged-in state. This could be their dashboard, a profile page, or any other personalized content. By redirecting the user after login, you can provide a seamless transition and ensure they have access to the right information.

To redirect to another page in PHP after login, we need to follow a few steps. Let’s break it down:

Step 1: Create a Login Form

The first step is to create a login form where users can enter their credentials. This form typically includes input fields for the username and password, along with a submit button. You can add some CSS styling to make it visually appealing.

Step 2: Handle Form Submission

Once the user submits the login form, we need to handle the form submission on the server-side. This is where PHP comes into play. In your PHP code, you’ll need to validate the user’s credentials against your database. If the login is successful, you can set a session variable to indicate that the user is logged in.

Here’s an example of how the PHP code might look like:

// Validate user credentials
if ($username === 'myUsername' && $password === 'myPassword') {
// Set session variable
$_SESSION['isLoggedIn'] = true;

Step 3: Perform the Redirect

Once the session variable is set to indicate that the user is logged in, we can redirect them to the desired page. In PHP, we can achieve this by using the header() function.

Here’s an example of how to redirect the user to a page called “dashboard.php”:

// Redirect to the dashboard page
header('Location: dashboard.php');

Note that the exit function is called after the redirect to ensure that no further code execution occurs.

Step 4: Handle Redirect Logic

On the destination page, such as the “dashboard.php” in our example, you can check if the user is logged in by accessing the session variable. If the user is not logged in, you can redirect them back to the login page.

// Check if the user is not logged in
if (!$_SESSION['isLoggedIn']) {
// Redirect back to the login page
header('Location: login.php');

By performing this check, you can ensure that only authenticated users have access to the protected content.

So, there you have it! You now know how to redirect to another page in PHP after login. By following these steps, you can create a seamless and secure login experience for your website users. Remember to validate user inputs, handle redirects properly, and ensure that your code is secure.


Redirecting users to another page after login is a fundamental feature in web development. It allows you to provide personalized content and enhance the user experience. By following the steps outlined in this article, you can implement this feature in your PHP applications with ease.

Now that you have the knowledge, go ahead and give it a try! Happy coding!