How To Create A Login Page In Html With Database

How To Articles

Creating a login page in HTML with a database is an essential skill for web developers. It allows users to securely access restricted areas of a website and provides a layer of protection for sensitive information. In this article, I will guide you through the process of creating a login page in HTML with a database, sharing my personal insights and tips along the way.

Setting up the Database

The first step in creating a login page with a database is to set up the database itself. For this task, we will be using a relational database management system like MySQL. Start by creating a new database and then create a table to store user information such as username and password.


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

Once you have the table structure ready, you can move on to the HTML part of the login page.

Building the Login Form

To create the login page interface, we will be utilizing HTML and CSS. Start by creating a new HTML file and add the necessary form elements for the login page.


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

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

<input type="submit" value="Login">
</form>

Make sure to specify the action attribute of the form tag to point to the PHP file that will handle the login logic.

Writing the PHP Logic

The PHP script will handle the authentication process by checking if the entered username and password match the ones stored in the database. Create a new PHP file called login.php and add the following code:


<?php
// Establish a database connection
$conn = mysqli_connect("localhost", "username", "password", "database");

// Retrieve user input
$username = $_POST['username'];
$password = $_POST['password'];

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

// Verify the password
if (password_verify($password, $user['password'])) {
// Successful login
echo "Welcome, " . $user['username'] . "!";
} else {
// Invalid login
echo "Invalid username or password.";
}

// Close the database connection
mysqli_close($conn);
?>

In this PHP script, we establish a connection to the database, retrieve the user input from the login form, and query the database to find a user with the entered username. We then use the password_verify function to verify the entered password against the hashed password stored in the database. If the login is successful, we display a welcome message; otherwise, we show an error message.

Conclusion

Creating a login page in HTML with a database is an important aspect of web development. By following the steps outlined in this article, you can create a secure and user-friendly login page that enables secure access to restricted areas of your website. Remember to handle user input with care and implement security measures such as password hashing to protect sensitive information.

Now that you have learned how to create a login page in HTML with a database, you can incorporate this knowledge into your own web development projects. Happy coding!