How to use php to make a login page jump page

PHPz
Release: 2023-04-03 15:16:02
Original
1503 people have browsed it

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.

    Login  
  

Login



Copy after login

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.

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');
Copy after login

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.

    Dashboard  
  

Dashboard

Welcome!

Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!