How to use PHP and CGI to implement user registration and login functions

PHPz
Release: 2023-07-21 14:32:01
Original
1521 people have browsed it

How to use PHP and CGI to implement user registration and login functions

User registration and login are one of the necessary functions for many websites. In this article, we will introduce how to use PHP and CGI to achieve these two functions. We'll demonstrate the entire process with a code example.

1. Implementation of the user registration function

The user registration function allows new users to create an account and save their information to the database. The following is a code example to implement the user registration function:

  1. Create a database table

First, we need to create a database table to store user information. You can create a table named "users" using the following SQL statement:

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

  1. Registration form page

Create a register.php file as the page for the user registration form. In this file, we will display a form with input boxes for username, password, and email.

<!DOCTYPE html>
<html>
<head>
    <title>用户注册</title>
</head>
<body>
    <h2>用户注册</h2>
    <form method="post" action="register.php">
        <input type="text" name="username" placeholder="用户名" required><br>
        <input type="password" name="password" placeholder="密码" required><br>
        <input type="email" name="email" placeholder="电子邮件" required><br>
        <input type="submit" value="注册">
    </form>
</body>
</html>
Copy after login
  1. Registration processing page

Create a register.php file to process the submission of the user registration form. In this file we will take the input values ​​from the form and insert them into the database.

<?php
// 连接到数据库
$connection = mysqli_connect("localhost", "username", "password", "database");

// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "数据库连接失败:" . mysqli_connect_error();
    exit();
}

// 获取表单中的输入值
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

// 插入用户信息到数据库
$query = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')";
mysqli_query($connection, $query);

// 关闭数据库连接
mysqli_close($connection);

echo "注册成功!";
?>
Copy after login

The above are the basic steps and code examples to implement the user registration function. After the user fills in the information in the registration form and clicks the registration button, the user information will be inserted into the database.

2. Implementation of user login function

The user login function allows registered users to log in using their username and password. The following is a code example to implement the user login function:

  1. Login form page

Create a login.php file as the user login form page. In this file, we will display a form with input boxes for username and password.

<!DOCTYPE html>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
    <h2>用户登录</h2>
    <form method="post" action="login.php">
        <input type="text" name="username" placeholder="用户名" required><br>
        <input type="password" name="password" placeholder="密码" required><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>
Copy after login
  1. Login processing page

Create a login.php file to process the submission of the user login form. In this file, we will take the input values ​​from the form and verify that the user's username and password match by querying the database.

<?php
// 连接到数据库
$connection = mysqli_connect("localhost", "username", "password", "database");

// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "数据库连接失败:" . mysqli_connect_error();
    exit();
}

// 获取表单中的输入值
$username = $_POST['username'];
$password = $_POST['password'];

// 查询数据库验证用户名和密码
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($connection, $query);

// 检查是否找到匹配的用户
if (mysqli_num_rows($result) == 1) {
    // 用户验证通过,执行登录操作
    echo "登录成功!";
} else {
    // 用户验证未通过
    echo "用户名或密码错误,请重试!";
}

// 关闭数据库连接
mysqli_close($connection);
?>
Copy after login

The above are the basic steps and code examples to implement the user login function. After the user enters the username and password in the login form and clicks the login button, the user's identity will be verified by querying the database.

Summary:

This article introduces how to use PHP and CGI to implement user registration and login functions. Through the above code examples, we can see the basic implementation steps of user registration and login functions, including creating database tables, creating registration and login form pages, and processing form submissions. I hope this article can help you understand and implement user registration and login functions!

The above is the detailed content of How to use PHP and CGI to implement user registration and login functions. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!