Home >Backend Development >PHP Problem >How to use PHP to determine if you are not logged in and jump
With the development of Internet technology, many websites need to use the user login function to distinguish different users and provide them with specific services. When developing a user login function on a website, it is crucial to design a login verification mechanism. If there is no corresponding verification mechanism, there will be security risks.
The following is a method to determine the user's login status, which is implemented using PHP language.
Define a login verification function in the header file of the website. This function determines whether the user is logged in by determining whether a specific session variable exists. If the session variable exists, it means that the user is logged in, otherwise it means that the user is not logged in. The pseudo code of this function is as follows:
function check_login() { if( !isset($_SESSION['username']) ) { header("location: login.php"); exit(); } }
Next, call the above function in the web page that requires login verification. If the user is not logged in, this function will redirect the user to the login page, which is the login.php page.
In the login.php page, users can enter their username and password and submit the form. The background PHP code will verify whether the user name and password are correct. If correct, the user information will be stored in the session variable to indicate that the user has logged in. The PHP code is as follows:
<?php session_start(); // 开启session if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { // 获取用户名和密码 $username = $_POST['username']; $password = $_POST['password']; // 判断用户名和密码是否正确,假设用户名密码正确 if( $username == 'admin' && $password == 'admin123' ) { $_SESSION['username'] = $username; // 将用户名存入session header("location: index.php"); // 登录成功重定向到 index.php exit(); } else { $msg = "用户名或密码错误,请重新输入!"; } } ?> <!-- 在HTML代码中显示错误信息 --> <html> <head> <title>Login Page</title> </head> <body> <h2>Login Page</h2> <?php if(isset($msg)) { ?> <p style="color:red;"><?php echo $msg; ?></p> <?php } ?> <form method="post"> <label>Username:</label> <input type="text" name="username"><br><br> <label>Password:</label> <input type="password" name="password"><br><br> <input type="submit" value="Login"> </form> </body> </html>
In the index.php page, we need to use the check_login()
function to determine whether the user has logged in. If the user is logged in, the content of the page will be displayed; if the user is not logged in, the function will redirect the user to the login.php page. The PHP code is as follows:
<?php session_start(); // 开启session include_once("auth.php"); // 包含头文件 check_login(); // 判断用户登录状态 ?> <html> <head> <title>Index Page</title> </head> <body> <h2>Index Page</h2> <p>Welcome, <?php echo $_SESSION['username']; ?> !</p> <a href="logout.php">Logout</a> </body> </html>
In summary, this method requires first defining the check_login() function in the header file and calling this function in the page that requires login verification. At the same time, the user login information needs to be stored in the session variable in the PHP code of the login verification page, and the session variable is used to determine the user's login status in the page that requires verification. This method is simple and easy to implement, while also providing good guarantees in terms of security and user experience.
The above is the detailed content of How to use PHP to determine if you are not logged in and jump. For more information, please follow other related articles on the PHP Chinese website!