Home >Backend Development >PHP Tutorial >How to ensure that the content is displayed normally after the PHP login page jumps
In PHP programs, it is very important to ensure that the content is displayed normally after the login page jumps. In order to implement this function, we need to use session control and conditional judgment in the PHP file to ensure that the user can see the corresponding content after logging in.
First, we need to create a simple login page and a PHP file that contains login verification. Suppose we have a login page login.php and a file checklogin.php that handles login authentication. Now let's write the contents of these two files.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login Page</title> </head> <body> <h2>Login Form</h2> <form action="checklogin.php" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username" required><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required><br><br> <input type="submit" value="Login"> </form> </body> </html>
<?php session_start(); // Simulate the username and password stored in the database $valid_username = 'admin'; $valid_password = '123456'; // Get the username and password entered by the user $username = $_POST['username']; $password = $_POST['password']; //Verify username and password if ($username === $valid_username && $password === $valid_password) { $_SESSION['username'] = $username; // Store username in session header('Location: dashboard.php'); // Jump to dashboard.php after successful login } else { echo 'Invalid username or password'; // Verification fails and error message is output. } ?>
<?php session_start(); if (!isset($_SESSION['username'])) { header('Location: login.php'); // If not logged in, jump to the login page } $username = $_SESSION['username']; // Get username from session ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dashboard</title> </head> <body> <h2>Welcome, <?php echo $username; ?></h2> <p>This is the dashboard page.</p> </body> </html>
In this code, we first create a simple login page login.php. The user needs to enter the username and password before submitting the form. Once submitted, the form data is sent to the checklogin.php file for validation. If the username and password match, the username is stored in the session and redirected to the dashboard.php page. In the dashboard.php page, we first start the session and use the user name stored in the session to determine whether the user is logged in. If not, jump back to the login page.
In this way, when the user logs in successfully, he can access the dashboard.php page and display the welcome message. This is how to ensure that the content is displayed properly after the login page jumps in PHP.
The above is the detailed content of How to ensure that the content is displayed normally after the PHP login page jumps. For more information, please follow other related articles on the PHP Chinese website!