How To Make A Login Page In Php With Database

In this article, I will guide you through the process of creating a login page in PHP with a database. As a web developer, I have encountered the need for secure user authentication on numerous occasions, and creating a login page is an essential part of this process. So, let’s dive right in!

Setting up the Environment

Before we start coding, we need to ensure that we have the necessary tools and environment set up for PHP development. Firstly, make sure you have PHP installed on your local machine. You can download the latest version of PHP from the official website.

Next, we need to set up a database to store user information. For this tutorial, we will be using MySQL. Install MySQL and a graphical user interface like phpMyAdmin to manage the database easily.

Creating the Database

Once you have MySQL installed, open phpMyAdmin and create a new database for our login page. Let’s name it “login_system”. Inside this database, create a table named “users” with the following columns:

  • id (INT, primary key, auto-increment)
  • username (VARCHAR)
  • password (VARCHAR)

Creating the Login Page

Now that our environment is set up and the database is ready, let’s start building the login page. Create a new PHP file and name it “login.php”. Inside this file, we will write the HTML and PHP code to create the login form.

Start by creating an HTML form with two input fields for username and password. Add a submit button to let users log in. Wrap the form in the <form> tags and set the action attribute to point to a PHP script that we will create later.

After the HTML form, we need to write the PHP code to handle the form submission. When the user submits the form, we will retrieve the input values, validate them, and check if they match the records in the database.

To connect to the database, we need to use the mysqli extension in PHP. Create a new PHP block and write the code to establish a database connection using the mysqli_connect() function. Make sure to provide the correct host, username, password, and database name in the function arguments.

Once the connection is established, we can retrieve the form input values using the $_POST superglobal variable. Sanitize the input values to prevent SQL injection and assign them to variables for further processing.

Next, we need to query the database and check if the username exists. Use the mysqli_query() function to execute a SELECT query on the “users” table, searching for a row with the given username. If a matching row is found, retrieve the corresponding password from the database.

Compare the retrieved password with the password entered by the user. If they match, it means the login is successful. Store the user’s information in session variables for future use and redirect them to a dashboard or home page.

Example PHP Code:


<?php
// Database connection
$conn = mysqli_connect("localhost", "username", "password", "login_system");

// Retrieve form input values
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);

// Query the database
$result = mysqli_query($conn, "SELECT * FROM users WHERE username = '$username'");
$row = mysqli_fetch_assoc($result);

// Check username and password
if ($row && password_verify($password, $row['password'])) {
// Login successful
session_start();
$_SESSION['username'] = $username;
$_SESSION['id'] = $row['id'];
header("Location: dashboard.php");
exit();
} else {
// Login failed
echo "Invalid username or password";
}
?>

Conclusion

Creating a login page in PHP with a database is crucial for any web application that requires user authentication. By following this step-by-step guide, you should now have a better understanding of how to implement a secure login system using PHP and MySQL.

Remember to always validate user input, sanitize data, and use secure password hashing algorithms like bcrypt to protect user passwords. Additionally, consider implementing features like password reset and account lockout for enhanced security.

Now that you have learned how to create a login page, you can expand upon this knowledge to build more advanced authentication systems and explore other features such as user registration and user roles.

Good luck with your PHP development journey!