Redirecting to a new page after successful login

Redirecting to a new page after successful login

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Redirecting to a new page after successful login

When a user submits credentials on login.php, the most critical moment is what happens next. If the authentication logic confirms the user exists and their password matches, simply displaying a "Login Complete" message and leaving them staring at the login form is poor user experience. They expect to be taken immediately to their dashboard or home page.

The standard approach for server-side rendering involves using HTTP redirects. Since your login.php handles the POST request containing credentials, you must issue a redirect after processing that data but before echoing any HTML content to the browser. The destination URL is your landing page (transfer.html).

Here is the specific logic required within your PHP script:

<?php
// ... existing database connection and query logic ...

if ($match > 0) {
    // Set the session variable for security, preventing CSRF
    $_SESSION['user_id'] = $UserID; 
    
    // Perform the redirect immediately
    header('Location: transfer.html');
    exit(); 
} else {
    $msg = 'Login Failed!<br />';
    $msg .= 'Please make sure that you enter the correct details.';
}

// Only display content if a message exists or redirect didn't happen
if (isset($msg) || !$match) {
    echo '<div class="statusmsg">'.$msg.'</div>';
}
?>

Why exit() is mandatory

The header() function sends an HTTP response header to the client. However, PHP will not send headers once any output has been sent to the browser. In your original code structure, if you accidentally echo a string or include a file before the redirect logic runs, you will encounter a "Headers already sent" error.

By placing header('Location: transfer.html'); immediately after the successful query check, you ensure the browser receives the instruction to navigate away from login.php. The subsequent exit(); (or die()) is crucial because it stops PHP execution. Without it, the script might continue running and attempt to render the "Login Complete" message or process the rest of the form elements, potentially causing duplicate content issues or security risks like Cross-Site Request Forgery (CSRF) if you don't validate tokens later.

Handling the session state

Your source code checks for a match but does not explicitly store user data in a session. Redirecting immediately without setting a session variable is risky. If transfer.html expects to know who is logged in, it will fail because the browser has moved away from the POST request context.

You should update your query logic to capture the authenticated user's ID or email:

if ($match > 0) {
    // Retrieve the UserID from the result set
    $row = mysql_fetch_assoc($search); 
    $_SESSION['UserID'] = $row['UserID'];
    
    header('Location: transfer.html');
    exit();
}

Security considerations with mysql_* functions

It is worth noting that your code utilizes the deprecated mysql_* extension. These functions are removed in PHP 7.0+. While this article focuses on the redirect mechanism, switching to PDO or MySQLi is a necessary step for any new project. Furthermore, using mysql_escape_string() directly in queries is prone to errors if not applied consistently. Prepared statements prevent SQL injection by separating data from code, rather than relying on string concatenation within the WHERE clause.

The "Flash" Message Pattern

If you want to acknowledge a successful login without redirecting immediately (for example, to show a success message before navigating), you can use a session flash message. However, for banking or sensitive applications where speed is key, a direct header redirect is often preferred to minimize the time a user spends on an unauthenticated page.

Once the browser processes the Location header, it requests transfer.html. Ensure that this file does not require any POST data from login.php, as the original form submission context will be lost once the redirect occurs.

Our network