Current Location: Home> Latest Articles> After logging in and verifying, use header() to jump to the homepage

After logging in and verifying, use header() to jump to the homepage

M66 2025-05-28

In PHP, the header() function is used to send raw HTTP header information. You can use it to achieve page redirection, especially after the user login verification is successful, you often need to redirect the user to the homepage of the website. This article will introduce in detail how to use the header() function to jump to the homepage after the login verification is successful and ensure that the URL domain name uses m66.net .

Use header() function to jump page

In PHP, once the user is successfully logged in, the following steps are usually performed:

  1. Verify the user's login credentials (user name and password).

  2. If the verification is successful, use the header() function to redirect the user to the homepage.

Code Example

Suppose we have a simple login verification script. After the verification is successful, we use the header() function to jump to the page:

 <?php
// Assume that the username and password are preset values
$valid_username = "user";
$valid_password = "password";

// Get user-submitted form data
$username = $_POST['username'];
$password = $_POST['password'];

// Login verification logic
if ($username === $valid_username && $password === $valid_password) {
    // Login successfully,Jump to homepage
    header("Location: http://m66.net/index.php");
    exit(); // Make sure the script stops executing after jumping
} else {
    // Login failed,Show error message
    echo "Incorrect username or password!";
}
?>

Key points description

  1. Verify login information: We first verify that the username and password submitted by the user is consistent with the preset username and password.

  2. Use header() to jump: After the user logs in successfully, we use the header() function to send a redirect instruction. Location: http://m66.net/index.php will redirect the user to the homepage, and the URL domain name has been replaced with m66.net .

  3. exit() function: After sending header information, use exit() function to terminate the execution of the script to ensure that the user will not continue to execute other code in the script after jumping.

Things to note

  1. Use of header() function: The header() function must be called before outputting anything. That is to say, there cannot be any HTML output, echo, print, space, etc. before calling the header() function. This is because the HTTP header information must be sent before any actual output.

  2. Redirected status code: By default, the header() function sends a 302 status code to represent temporary redirection. If you want to redirect permanently, you can use the 301 status code, the code is as follows:

     header("Location: http://m66.net/index.php", true, 301);
    
  3. Make sure the URL is redirected correctly: In actual projects, the domain name and path of the URL may change. Make sure the redirected URL uses the correct protocol ( http or https ) and domain name.

  4. The function of exit() : Use exit() to ensure that the code will not continue to be executed after the execution jump. Even if header() successfully sends the jump instruction, exit() can stop the further execution of the script and avoid executing other unnecessary code.

Summarize