Hello there! In this tutorial, I will guide you through the steps of building a basic login page in PHP using a database as the source code. As a web developer, login pages are crucial for websites that need user verification. Let’s get started!
Setting Up the Database
The first step in creating a login page with PHP is to set up the database. For this tutorial, we will be using MySQL as our database management system. Start by creating a new database in MySQL and name it something like “login_system”. Inside this database, create a table called “users”.
The “users” table will have three columns: “id”, “username”, and “password”. The “id” column will be an auto-incrementing integer, while the “username” and “password” columns will be of type VARCHAR.
Here is an example of the SQL code to create the “users” table:
CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
Creating the HTML Form
Now that we have our database set up, let’s create the HTML form for our login page. Open a new PHP file and add the following code:
<form action="login.php" method="POST">
<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="Login">
</form>
In this form, we have two input fields: one for the username and one for the password. The form will be submitted to a file called “login.php” using the POST method.
Processing the Form Data
Next, let’s create the “login.php” file that will handle the form data and check if the user’s credentials are valid. Inside this file, we will perform the following steps:
- Connect to the database
- Retrieve the username and password from the form
- Check if the username exists in the database
- If the username exists, verify the password
- Redirect the user to a secure page if the login is successful
- Display an error message if the login is unsuccessful
Here is an example of the code to process the form data:
<?php
// Step 1: Connect to the database
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "login_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Step 2: Retrieve the username and password from the form
$username = $_POST["username"];
$password = $_POST["password"];
// Step 3: Check if the username exists in the database
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
// Step 4: Verify the password
$row = $result->fetch_assoc();
if (password_verify($password, $row["password"])) {
// Step 5: Redirect the user to a secure page
header("Location: secure_page.php");
exit();
} else {
// Step 6: Display an error message
echo "Invalid password";
}
} else {
// Step 6: Display an error message
echo "Invalid username";
}
$conn->close();
?>
Conclusion
And there you have it! You have successfully created a simple login page in PHP with a database source code. By following the steps outlined in this tutorial, you can create a secure login system for your website. Remember to always hash and salt your passwords for maximum security. Happy coding!