Home > Backend Development > PHP Tutorial > How to develop a simple login system using PHP

How to develop a simple login system using PHP

WBOY
Release: 2023-09-25 09:24:01
Original
1369 people have browsed it

How to develop a simple login system using PHP

How to use PHP to develop a simple login system

Introduction:
In Web development, the login system is a very common function. This article will introduce how to write a simple login system using PHP. Through this system, users can enter their username and password to log in, and can perform some operations after successful login.

1. Preparation work
Before we start writing the login system, we need to set up a basic development environment. First, make sure your computer has PHP and related development environments (such as Apache, MySQL, etc.) installed. Secondly, create a new project directory and create a folder named "login_system" under this directory. Finally, open a text editor (such as Notepad, Sublime Text, etc.) and start writing code.

2. Create a database table
Before we start writing PHP code, we need to create a database table to store user information. We can use the MySQL database to create a table named "users", which contains three fields: id (user ID, self-increasing), username (user name) and password (password).

CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);

3. Write HTML page
First, we create a file named "index.html" and write HTML code in it. This file will be used to display the login form.




Log in to the system


Welcome to log in














4. Write PHP code
Next, we need to create A file named "login.php" and write PHP code in it to handle the login request.

// Get the username and password entered by the user
$username = $_POST['username'];
$password = $_POST['password'];

//Connect to the database
$servername = "localhost";
$dbname = "login_system";
$dbusername = "root";
$dbpassword = "";

$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);

// Check whether the database connection is successful
if ($conn->connect_error) {

die("数据库连接失败: " . $conn->connect_error);
Copy after login

}

//Query whether the user exists in the database
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

// 登录成功
echo "登录成功!";
Copy after login

} else {

// 登录失败
echo "用户名或密码错误!";
Copy after login

}

//Close the database connection
$conn->close();
?>

5. Test the login system
Complete the above code Finally, we can run the login system in a browser for testing. Open the browser and enter http://localhost/login_system/index.html to enter the login page. Enter the corresponding values ​​in the username and password input boxes and click the "Login" button. The system will verify based on the entered user name and password and give corresponding prompts.

Summary:
It is not complicated to develop a simple login system using PHP. Through the code examples provided in this article, you can understand how to create a database table and write HTML and PHP code to implement the login function. Of course, this is just a simple example. If you have more complex needs, you can further expand the functionality. I hope this article will be helpful to you and help you better understand the login system in PHP development.

The above is the detailed content of How to develop a simple login system using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template