Home  >  Article  >  Backend Development  >  How to use php to make a login page jump page

How to use php to make a login page jump page

PHPz
PHPzOriginal
2023-04-03 14:09:541532browse

In the field of website development, user login system is a very common requirement. In a website, the core parts of the user login system are the login page, user authentication and jump pages. This article will introduce how to use PHP to implement a basic login page and display the user's session status by jumping to the page.

In this article, we will use the following three basic files: login.php, authenticate.php and dashboard.php.

  1. login.php file

The login page is the entry point for users to log into the system. We will implement this page in the login.php file.

<?php
session_start();
if (isset($_SESSION[&#39;user_id&#39;])) {
    header(&#39;Location: dashboard.php&#39;);
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <meta charset="utf-8">
</head>
<body>
    <h1>Login</h1>
    <form method="post" action="authenticate.php">
        <label for="user_name">Username:</label>
        <input type="text" name="user_name"><br>
        <label for="password">Password:</label>
        <input type="password" name="password"><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>

The above code shows a simple HTML page. It includes a form where users can enter their username and password. Submission of the form will be sent to the authenticate.php page, which will be responsible for comparing the data entered by the user with the data stored in the database to confirm whether the user has permission to access the protected resource.

The code also starts a new session using the session_start() function. If the user is already logged in, the session_start() function will check if the user ID exists in the current session, and if so, redirect directly to the dashboard.php page.

  1. authenticate.php file

authenticate.php file will be responsible for the user's authentication process. Basically it will compare the user credentials stored in the database with the credentials provided by the user on the login page. If they match, it creates a new session and stores the user ID in $_SESSION['user_id']. It will then redirect to the dashboard.php page.

In this example, we assume that the username and password are already stored in a database table named users.

<?php
session_start();
if (isset($_SESSION[&#39;user_id&#39;])) {
    header(&#39;Location: dashboard.php&#39;);
    exit();
}

if (empty($_POST[&#39;user_name&#39;]) || empty($_POST[&#39;password&#39;])) {
    header(&#39;Location: login.php&#39;);
    exit();
}

$user_name = $_POST[&#39;user_name&#39;];
$password = $_POST[&#39;password&#39;];

$db = new PDO(&#39;mysql:host=localhost;dbname=mydatabase;charset=utf8mb4&#39;, &#39;username&#39;, &#39;password&#39;);
$stmt = $db->prepare("SELECT id FROM users WHERE user_name=:user_name AND password=:password");
$stmt->execute(array(':user_name' => $user_name, ':password' => $password));
$user = $stmt->fetch();

if (!$user) {
    header('Location: login.php');
    exit();
}

$_SESSION['user_id'] = $user['id'];
header('Location: dashboard.php');

The execution flow of the above code is as follows:

First, we check the value of $_SESSION['user_id']. If it exists, it means that the user has logged in and is trying to log in again. In this case we will redirect to dashboard.php page.

Next, we check if the user provided a username and password in the login form. If not, the login process is considered invalid. In this case we will redirect to the login.php page.

Next, we look up the username and password provided by the user in the database. We use PDO PHP extension to connect to MySQL database. For a given username and password, we will check if the corresponding row exists in the users table. If present, the user is authenticated.

Finally, we create a new $_SESSION['user_id'] variable, which stores the user's ID and redirects to the dashboard.php page.

  1. dashboard.php file

dashboard.php file will display the user's session status. If the user has been authenticated, the dashboard.php page will read the $_SESSION['user_id'] variable and display a welcome message.

<?php
session_start();
if (!isset($_SESSION[&#39;user_id&#39;])) {
    header(&#39;Location: login.php&#39;);
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
    <meta charset="utf-8">
</head>
<body>
    <h1>Dashboard</h1>
    <p>Welcome!</p>
</body>
</html>

The execution flow of the above code is as follows:

First, we check the value of the $_SESSION['user_id'] variable. If the variable does not exist, it means that the user has not been authenticated. In this case we will redirect to the login.php page.

Next, we display a welcome message indicating that the user has been authenticated and can access the protected resource.

In this article, we have learned how to use PHP to implement a basic login system. The system uses basic HTML and PHP syntax, as well as MySQL and PDO extensions. Through this example, you have learned how to create an HTML form, read and write data in a MySQL database, create a new session, and inspect variables in the session. These skills are all necessary to build more complex web applications.

The above is the detailed content of How to use php to make a login page jump page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn