How To Make Login Page In Php With Database

Creating a login page in PHP with a database is an essential aspect of developing web applications that require user authentication. In this article, I will guide you through the process of building a secure and user-friendly login page using PHP and a database.

Setting up the Database

The first step is to set up the database that will store user information. You can use any preferred database management system like MySQL or PostgreSQL. In this example, I will be using MySQL.

To begin, create a new database and a table to store user information. The table should have columns for the user’s ID, username, password, and any other relevant information.


CREATE DATABASE login_system;
USE login_system;

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

Creating the Login Form

Now, let’s move on to creating the login form. Start by creating a new PHP file and adding the HTML code for the login form. The form should include input fields for the username and password, along with a submit button.


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

Connecting to the Database

In order to authenticate the user’s credentials, we need to establish a connection to the database within our PHP code. Create a new PHP file named “db_connect.php” and add the following code:


<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "login_system";

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

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

Make sure to replace “your_username” and “your_password” with your actual database credentials.

Handling the Login Request

In the “login.php” file, which is the action of the login form, we will handle the login request. Start by including the “db_connect.php” file to establish a database connection:


<?php
session_start();
include 'db_connect.php';

if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT id, username, password FROM users WHERE username = '$username'";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if (password_verify($password, $row['password'])) {
$_SESSION['user_id'] = $row['id'];
$_SESSION['username'] = $row['username'];
header("Location: home.php");
exit();
} else {
echo "Invalid username or password.";
}
} else {
echo "Invalid username or password.";
}
}
?>

The code above checks if the username and password fields are filled. It then queries the database to find a matching user based on the entered username. If a match is found, it verifies the password using the PHP password_verify() function. If the verification is successful, it sets session variables and redirects the user to the home page. Otherwise, it displays an error message.

Enhancing Security

When dealing with user authentication, it is crucial to ensure the highest level of security. Here are a few additional steps you can take to enhance security:

  • Use HTTPS to encrypt the communication between the user and the server.
  • Hash the passwords using a strong hashing algorithm like bcrypt.
  • Implement measures to prevent or limit brute force attacks, such as rate limiting and account lockouts.

Conclusion

Creating a login page in PHP with a database is an important skill for web developers. By following the steps outlined in this article, you can build a secure and reliable login system for your web applications. Remember to always prioritize security and stay updated with the latest best practices to protect user information.

Feel free to experiment and add your personal touches to make the login page fit your specific needs. Good luck with your PHP login page development!