How To Create A Login Page Using Php And Mysql

Creating a login page is a crucial step when building a website that requires user authentication. In this article, I will guide you through the process of creating a login page using PHP and MySQL. As a web developer, I have found this method to be secure and efficient.

Prerequisites

Before we dive into the code, make sure you have the following:

  • A web server with PHP support
  • MySQL installed
  • Basic knowledge of PHP and MySQL

Step 1: Database Setup

The first step is to set up the database that will store user credentials. Start by creating a new database in MySQL. You can use a tool like phpMyAdmin or run the following SQL query:


CREATE DATABASE login_system;

Next, create a table to store user information. Run the following SQL query:


CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

Step 2: HTML Form

Now let’s create the login form. Open your code editor and create a new PHP file, such as login.php. Add the following code:


<form method="POST" action="login.php">
<label for="username">Username:</label>
<input type="text" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" name="password" required>
<br>
<input type="submit" value="Login">
</form>

Step 3: PHP Script

Now let’s handle the form submission and validate the user credentials. In the same login.php file, add the following PHP code:


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

// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "login_system");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Check if the username and password match
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
if (password_verify($password, $row["password"])) {
echo "Login successful!";
} else {
echo "Invalid username or password.";
}
} else {
echo "Invalid username or password.";
}

mysqli_close($conn);
}
?>

Step 4: User Registration

If you want users to be able to register themselves, you can create a separate registration form and script. The registration form should have fields for username and password, and the PHP script should insert the new user’s information into the database. Remember to hash the password for security.

Conclusion

Creating a login page using PHP and MySQL is an essential skill for web developers. By following the steps in this article, you have learned how to set up a database, create an HTML form, handle form submission with PHP, and validate user credentials. Remember to always prioritize the security of user information by using proper encryption and following best practices.

Now that you have a solid foundation, feel free to add personal touches and additional features to enhance your login page. Happy coding!