Current Location: Home> Latest Articles> How to realize redirection after user logout through header()

How to realize redirection after user logout through header()

M66 2025-06-01

In web development, users usually need to redirect pages when the user logs in. Usually this operation is done through the header() function. The header() function can send the original HTTP header information, and after the user logs out, it can use it to jump the page. This article will introduce how to use PHP's header() function to achieve automatic redirection after user logout.

What is the header() function?

The header() function in PHP is used to send raw HTTP header information. These header information can control the behavior of the browser, including redirection, cache control, content type, etc. Its basic syntax is as follows:

 header(string $header, bool $replace = true, int $response_code = 0)
  • $header : Required, specifying the header information to be sent.

  • $replace : optional, indicating whether to replace the header information of the same type sent previously. Default is true .

  • $response_code : optional, representing the HTTP response code, which is usually used to set the status code.

Log out and redirect

In PHP, user logout is usually achieved by clearing session data (such as $_SESSION ) or clearing cookies. After logging out, we can automatically redirect to the specified page through the header() function.

Here is a simple example that demonstrates how to automatically redirect to the homepage after the user is logged out through the header() function:

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

// Clear session data
session_unset();
session_destroy();

// Redirect to homepage
header("Location: http://m66.net/index.php");
exit();
?>

Explain code:

  1. Start the session : Use the session_start() function to start the session to ensure that we can access the $_SESSION array.

  2. Clear session data : The session_unset() function clears all variables in the current session. session_destroy() destroys the session, making session data no longer available.

  3. Redirection : Use the header() function to send the Location header information, instructing the browser to redirect to http://m66.net/index.php to achieve page redirection.

  4. Terminate script execution : Call the exit() function to ensure that the script will not continue to be executed after sending the HTTP header.

Things to note

  1. Call order : The header() function must be called before outputting anything, including HTML, spaces, line breaks, etc. If there is already an output before calling header() , PHP will report an error and cannot send HTTP header information.

  2. Using absolute URLs : When redirecting, we usually use absolute URLs (including protocols such as http:// ). This is to ensure that redirection can be performed correctly even if the directory structure of the page changes.

  3. Avoid redirect loops : When designing redirect logic, make sure you don't get stuck in a redirect loop (for example, after logging out the user and redirecting the user to the logout page, which in turn redirects the user to the logout page).

summary

Use PHP's header() function to easily implement page redirection after user logout. By clearing session data and sending Location header information, we can automatically jump to the specified page after the user logs out, thereby improving the user experience. When using header() for redirection, pay attention to the call order, avoid output interference, and ensure the correctness of the URL.