How to use php to implement user registration and login

PHPz
Release: 2023-04-19 09:27:35
Original
1052 people have browsed it

Use PHP to implement user registration and login

In the modern Internet era, user registration and login are an inevitable process. As a web developer, it is very important to understand how to build user registration and login systems. This article will introduce how to use PHP to implement user registration and login.

Step 1: Create a database

Before we start, we need to create a database. We will use MySQL database. Create a table named "users" in MySQL that contains the id, username, and password fields. We can use the following SQL statement to create a table.

CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL ,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Step 2: Create a registration form

Now we need to create a user registration form. We will use HTML and PHP code to build the form.



<title>User Registration</title>
Copy after login


<h2>User Registration</h2>
<form action="register.php" method="post">
    <label>Username:</label><br>
    <input type="text" name="username" required><br>
    <label>Password:</label><br>
    <input type="password" name="password" required><br>
    <input type="submit" value="Register">
</form>
Copy after login


The above code creates an HTML form that contains username and password input boxes and a submit button. We set the "method" attribute to "post" and submit the form to "register.php".

Step 3: Process the registration form submission

Now we need to write the "register.php" file to process the registration form submission and store the user information in the database.

//Connect to the database
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabase";

$conn = new mysqli($servername, $username, $password, $dbname);

//Check database connection
if ($ conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
Copy after login
Copy after login

}

//Get the value submitted by the form
$username = $_POST['username'];
$password = password_hash( $_POST['password'], PASSWORD_DEFAULT);

//Insert records into the database
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password') ";

if ($conn->query($sql) === TRUE) {

echo "Registration successful";
Copy after login

} else {

echo "Error: " . $sql . "<br>" . $conn->error;
Copy after login

}

$conn->close();
?>

The above code first connects to the database. It then gets the username and password from the submitted form value and stores the password hash into the database using the "password_hash" function. Finally, it executes a SQL statement to insert the username and hashed password into the "users" table.

Step 4: Create a login form

Now we need to create a user login form to verify the user's identity. We will use HTML and PHP code to create the form.



<title>User Login</title>
Copy after login


<h2>User Login</h2>
<form action="login.php" method="post">
    <label>Username:</label><br>
    <input type="text" name="username" required><br>
    <label>Password:</label><br>
    <input type="password" name="password" required><br>
    <input type="submit" value="Login">
</form>
Copy after login


The above code creates an HTML form that contains username and password input boxes and a submit button. We set the "method" attribute to "post" and submit the form to "login.php".

Step 5: Process the login form submission

Now we need to write the "login.php" file to process the login form submission and verify the user's identity.

//Connect to the database
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabase";

$conn = new mysqli($servername, $username, $password, $dbname);

//Check database connection
if ($ conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
Copy after login
Copy after login

}

//Get the value submitted by the form
$username = $_POST['username'];
$password = $_POST ['password'];

//Retrieve user information from the database
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = $conn-> ;query($sql);

if ($result->num_rows == 1) {

$row = $result->fetch_assoc();
if (password_verify($password, $row['password'])) {
    echo "Login successful";
} else {
    echo "Invalid username or password";
}
Copy after login

} else {

echo "Invalid username or password";
Copy after login

}

$conn->close();
?>

The above code first connects to the database. It then gets the username and password from the submitted form values. Next, it executes a SELECT statement to retrieve the record containing that username from the "users" table. If a record is found, the password hash of that record is compared to the submitted password. If the passwords match, output "Login successful", otherwise output "Invalid username or password".

Summary

In this article, we use PHP and MySQL database to implement the user registration and login system. In this article, we learned how to:

  • Establish a MySQL database and create the "users" table;
  • Create user registration and login forms;
  • Use PHP to process form submissions and store user information in the database;
  • Verify user identity and output corresponding information.

In practical applications, we can modify and expand it as needed.

The above is the detailed content of How to use php to implement user registration and login. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template