Php

User authentication is a process of validating users with some keys, token or any other credentials. If the user gives correct credentials then the authentication process will be succeeded.

Bạn đang xem: Php

After successful authentication, the users will be allowed lớn the system as authenticated users.

In this tutorial, we are going khổng lồ create a PHPhường user authentication system with MySQL database. It will be easy khổng lồ understand this example code. By reading this article, you will learn how to code PHP user authentication.

Also, I have added the downloadable source code at the kết thúc of this article.

In this user authentication example, it has a login panel to lớn let users enter their login details. In this example, the username and password from the registered users’ database are needed for authentication. By submitting these login credentials, it will be posted lớn a PHPhường page.

In PHPhường. code, it executes a SELECT query to check if a user found in the database with the entered login credentials. If a match found, then the authentication process will be cleared by the user who attempts login. After successful login, the authenticated user will be allowed lớn enter inkhổng lồ the application.

*

Steps khổng lồ create a user authentication system in PHP..

Create a MySquốc lộ database with users table.Create a user login panel to submit login details to lớn PHP..Generate query khổng lồ compare user login details with the MySquốc lộ user database.

1) Create a MySquốc lộ Database with Users Table.

For creating a database & the database tables you can run the MySquốc lộ CREATE statement. Instead, you can go with any database client like SQLYog, PHPMyAdmin. I am using PHPMyAdmin for creating database & table structure that is used for the MySQL examples.

Xem thêm: Phần Mềm Hola Chrome Năm 2021 (+1 Cần Tránh), Hola Cho Chrome

In this example, I created a database named as payload for creating users table. The following screenshot shows the users table structure with data.

*

2) Create User Login Panel.

The following HTML code is used to show user login panel khổng lồ the users to enter their authentication details. It has two input fields for getting usernamepassword from the users. Once the user entered their login details and submitted the form, the username và password data will be posted to the PHP. to lớn process authentication with MySQL database.


Enter Login Details
These styles are added for the user authentication size elements by including a CSS tệp tin. The CSS provides a minimal look & feel to the PHP. user authentication UI. It is just a skeleton and could be changed easily for application theme.

bodyfont-family: calibri;.tblLogin border: #95bee6 1px solid; background: #d1e8ff; border-radius: 4px;.tableheader font-size: 24px; .tableheader td padding: 20px; .tablerow td text-align:center; .message color: #FF0000;font-weight: bold;text-align: center;width: 100%;.login-input đầu vào border: #CCC 1px solid; padding: 10px 20px;.btnSubmit padding: 10px 20px; background: #2c7ac5; border: #d1e8ff 1px solid; color: #FFF;

3) Generate Query to lớn Compare User Input with the Database.

In the following PHPhường code, it checks the $_POST global array length before executing the authentication code block. Once the user authentication khung is submitted, then this global array will contain the values of the size đầu vào fields.

In the PHPhường authentication code, it connects the MySquốc lộ database by specifying the configurations to get the connection object.

After receiving user authentication details in PHPhường., it compares the khung data with the user database by executing a query by using the connection object. In this query, it uses the username and password entered by the user to kiểm tra if any match found.

If match found, it means the user is a valid user who registered already with the system. So, the authentication code will allow the user to lớn proceed further.

No matter whether the authentication is cleared or not. Anyhow this code will acknowledge the user by displaying success or warning based on the result of the authentication process..

0) $conn = mysqli_connect("localhost","root","","obatambeienwasirherbal.com_examples");$result = mysqli_query($conn,"SELECT * FROM users WHERE user_name="" . $_POST<"userName"> . "" và password = "". $_POST<"password">.""");$count = mysqli_num_rows($result);if($count==0) $message = "Invalid Username or Password!"; else $message = "You are successfully authenticated!";?>Download