Current Location: Home> Latest Articles> The complete process of judging login status and then redirecting with isset()

The complete process of judging login status and then redirecting with isset()

M66 2025-05-28

Login verification is a common requirement in web development. When a user visits a page that needs to be logged in to view, we usually need to determine whether the user has logged in and make corresponding processing based on the judgment results. If the user is not logged in, we need to redirect to the login page. At this time, the combination of the header function and isset() function in PHP is a very practical tool.

This article will explain in detail how to use the header() function and isset() to determine the login status and perform the complete process of redirecting.

1. Understand the header() function and isset() function

1.1 header() function

The header() function is used to send the original HTTP header information. Through this function, we can set HTTP status code, content type, cache control and other information, and can also perform page redirection.

Common usages are:

 header("Location: http://www.m66.net/login.php");
exit;

The above code will redirect the browser to http://www.m66.net/login.php . exit; is to ensure that the script stops execution after redirection to avoid interference from subsequent code.

1.2 isset() function

The isset() function is used to detect whether a variable has been set and its value is not null . It is often used to determine whether the user has been logged in or whether a form field has been submitted.

 if (isset($_SESSION['user_id'])) {
    // User logged in
}

2. Basic ideas for judging login status

In PHP, the login status is usually maintained through a Session. When the user logs in, we will save the user's relevant information in the Session, such as user_id . On the page where you need to check the user's login status, we can use isset() to determine whether the Session variable exists. If it exists, it means that the user is logged in; if it does not exist, it means that the user is not logged in and redirection is required.

3. Complete login status check and redirection process

3.1 Initialize Session

First, make sure to start Session at the top of the page. You can use session_start() to start the session. This function must be called before any output.

 <?php
session_start();
?>

3.2 Determine whether the user is logged in

Next, we can use isset() to determine the user's login status. Suppose we save user_id in the Session.

 <?php
if (!isset($_SESSION['user_id'])) {
    // User not logged in,Redirect to login page
    header("Location: http://www.m66.net/login.php");
    exit;
}
?>

The above code implements login status check. If $_SESSION['user_id'] does not exist (i.e. the user is not logged in), header() will be executed for page redirection to the login page.

3.3 Processing the login page

For users who are already logged in, we can allow them to access the target page, or display welcome information, etc. For example:

 <?php
if (isset($_SESSION['user_id'])) {
    echo "welcome,user " . $_SESSION['user_id'];
}
?>

3.4 Comprehensive Example

Here is a complete example showing how to combine header() and isset() to determine login status and redirect it:

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

// 判断user是否登录
if (!isset($_SESSION['user_id'])) {
    // 如果user没有登录,Jump to login page
    header("Location: http://www.m66.net/login.php");
    exit; // Stop executing subsequent code after redirection
}

// user已经登录,显示welcome信息
echo "welcome您," . $_SESSION['user_id'] . "!";
?>

4. Things to note

  • The header() function needs to be called before any output : because header() is used to modify HTTP header information, there cannot be any output (including HTML tags, spaces, echo, print, etc.) before calling header() . If there is already an output, PHP will report an error: "Cannot modify header information - headers already sent".

  • exit function : After calling header("Location: ...") , exit; should be used immediately to prevent subsequent code from continuing execution, so as to ensure that the redirection will not be interfered with by other operations.

  • Session Management : In order to ensure the correctness of Session data, each page needs to use session_start() to start the session. If there are multiple places on the page that need to determine the login status, you can extract the login judgment code into a separate PHP file and include it by including .

5. Summary

This article introduces how to use the header() function and isset() function in PHP to determine the login status and perform page redirection. Save login information through Session and determine whether it exists through isset(). Redirecting operations with header() function can effectively control user access rights and ensure that only logged-in users can access restricted content.

This method is suitable for most web application scenarios that require login verification. I hope this article will be helpful to you.