How To Create A Login Page With Php And Mysql

Creating a login page with PHP and MySQL is an essential step in building a secure website or web application. In this article, I will guide you through the process of creating a login page from scratch, incorporating personal touches and providing detailed explanations along the way.

Setting up the Database

Before diving into the coding part, we need to set up a MySQL database to store user information such as usernames and passwords. Start by creating a new database and a table within it. Let’s call the table “users” and give it three columns: “id” (an auto-incrementing number), “username” (for storing usernames), and “password” (for storing hashed passwords).


CREATE DATABASE mydb;
USE mydb;

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

Creating the HTML Form

Now that the database is set up, let’s move on to creating the HTML form for the login page. Start by creating a new PHP file and adding the following code:


<form method="POST" action="login.php">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br>

<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br>

<input type="submit" value="Log In">
</form>

This code creates a simple form with two input fields: one for the username and one for the password. The form submits the data to a PHP file named “login.php” for processing.

Processing the Form Data

In the “login.php” file, we will write the code to process the form data and validate the user’s credentials. Here’s an example of how you can do this:


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];

// Connect to the MySQL database
$mysqli = new mysqli("localhost", "username", "password", "mydb");

// Check for connection errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}

// Prepare and execute the SQL query
$stmt = $mysqli->prepare("SELECT id, password FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($id, $hashedPassword);
$stmt->fetch();
$stmt->close();

// Verify the password
if (password_verify($password, $hashedPassword)) {
echo "Login successful";
} else {
echo "Invalid username or password";
}

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

This code checks if the HTTP request method is POST, retrieves the username and password from the form data, and connects to the MySQL database. It then prepares and executes an SQL query to retrieve the user’s hashed password from the database. The password_verify() function is used to compare the entered password with the stored hashed password. If the credentials match, the login is successful; otherwise, an error message is displayed.

Conclusion

Creating a login page with PHP and MySQL allows you to securely authenticate users and control access to your website or web application. By following the steps outlined in this article, you can build a robust login system that protects user data and provides a seamless login experience.

Remember to always prioritize security when implementing user authentication. Use techniques like password hashing and SQL parameterization to protect against common vulnerabilities such as password breaches and SQL injection attacks. With a solid understanding of PHP and MySQL, you can create a login page that meets your specific needs and provides a secure environment for your users.