Login Page In Php And Mysql

PHP Programming

In my time as a web developer, I have found that building a PHP and MySQL login page is crucial in creating a website that is both secure and user-friendly. This feature grants users access to restricted content and guarantees that only approved individuals can handle sensitive information. In this article, I will provide you with a step-by-step tutorial on creating and integrating a login page using these powerful tools.

Setting up the Database

Before diving into the PHP code, it is important to set up the MySQL database that will store user information. Start by creating a table to hold user credentials, such as username and password. You can use phpMyAdmin or any other MySQL administration tool to accomplish this task.


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

Once you have created the table, you can insert some sample users into the database. This will allow you to test the login functionality later on.


INSERT INTO users (username, password) VALUES ('john_doe', 'password123');
INSERT INTO users (username, password) VALUES ('jane_smith', 'securepass');

Designing the Login Form

Now that your database is ready, it’s time to design the login form. You can create an HTML form with two input fields: one for the username and another for the password. Additionally, include a submit button to trigger the authentication process. Here’s an example:


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

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

<input type="submit" value="Log In">
</form>

Remember to replace “login.php” with the appropriate filename if you decide to save the code into a separate file.

Authenticating User Credentials

Once the user submits the form, the data needs to be validated against the information stored in the database. This is where PHP comes into play.


<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve user input
$username = $_POST["username"];
$password = $_POST["password"];

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database_name");

// Check for connection errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}

// Prepare and execute the SQL statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();

// Check if a user was found
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();

// Verify the password
if (password_verify($password, $row["password"])) {
// Authentication successful
$_SESSION["username"] = $row["username"];
header("Location: welcome.php");
exit();
} else {
// Invalid password
echo "Invalid username or password.";
}
} else {
// User not found
echo "Invalid username or password.";
}

// Close the database connection
$mysqli->close();
}
?>

Make sure to replace “username”, “password”, and “database_name” with the actual credentials for your MySQL server.

Creating the Welcome Page

Upon successful authentication, it is common to redirect the user to a welcome page. This page can display personalized information or provide access to restricted content. Here’s a simple example:


<?php
session_start();

// Verify if the user is logged in
if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit();
}

// Display a welcome message
echo "Welcome, " . $_SESSION["username"] . "!";
?>

Conclusion

Designing and implementing a login page in PHP and MySQL is an essential skill for any web developer. By following the steps outlined in this article, you can create a secure and user-friendly login system for your website. Remember to apply best practices, such as securely storing passwords using techniques like password hashing and salting, to ensure the protection of your users’ data. With the right attention to detail and a focus on user experience, you can create a seamless login experience for your website visitors.