How To Make A Basic Php Login Page

Creating a basic PHP login page is an essential skill for any web developer. In this article, I will guide you through the process of building a simple login page using PHP. I will also add my personal touches and commentary along the way.


As a web developer, I understand the importance of having a secure login system for websites. A login page allows users to access restricted content and provides a layer of protection for sensitive information. By creating a PHP login page, you can ensure that only authorized users can access certain areas of your website.

Step 1: Setting Up the HTML Form

The first step in creating a PHP login page is to set up the HTML form that will collect user credentials. Start by creating a new HTML file and adding the following code:

<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="Login">

In the code above, we have created an HTML form with two input fields: one for the username and one for the password. The form’s method is set to “POST” and the action attribute is set to “login.php”. This means that when the user submits the form, the data will be sent to the “login.php” file for processing.

Step 2: Processing the Form Data

Now that we have our HTML form set up, let’s move on to processing the form data using PHP. Create a new PHP file called “login.php” and add the following code:

$username = $_POST["username"];
$password = $_POST["password"];

// Add your authentication logic here

if ($authenticated) {
// Redirect the user to the home page
header("Location: home.php");
} else {
$error = "Invalid username or password";

In the code above, we first check if the form has been submitted using the $_SERVER[“REQUEST_METHOD”] variable. If it is a POST request, we retrieve the values of the username and password fields using the $_POST superglobal variable.

At this point, you would add your own authentication logic to check if the username and password are valid. This could involve querying a database, comparing the credentials against a list of stored users, or using a third-party authentication service. For the sake of simplicity, let’s assume we have a simple array that contains valid usernames and passwords:

$users = [
"username" => "john",
"password" => "password123"
"username" => "jane",
"password" => "qwerty456"

$authenticated = false;

foreach ($users as $user) {
if ($user["username"] == $username && $user["password"] == $password) {
$authenticated = true;

In the code above, we loop through the array of users and check if the inputted username and password match any of the stored credentials. If a match is found, we set the $authenticated variable to true.

If the user is authenticated, we can redirect them to the home page using the header() function. Otherwise, we set an error message to be displayed on the login page.

Step 3: Handling the Login Result

Now that we have processed the form data and checked the authentication, let’s handle the login result. Modify your “login.php” file to include the following code:

<!DOCTYPE html>
<title>Login Result</title>
<h2>Login Result</h2>

<?php if ($authenticated) : ?>
<p>Welcome, <?php echo $username; ?>! You have successfully logged in.</p>
<?php else : ?>
<p><?php echo $error; ?></p>
<p>Please check your username and password and try again.</p>
<?php endif; ?>


In the code above, we first check if the user is authenticated. If they are, we display a welcome message with their username. If not, we display the error message that was set earlier along with a suggestion to check the username and password.


Creating a basic PHP login page is an important step in building secure websites. By following the steps outlined in this article, you can create a login page that collects user credentials, processes the data using PHP, and handles the login result accordingly. Remember to implement proper security measures, such as using password hashing and safeguarding against SQL injection, to ensure the security of your login system.

Now that you have a better understanding of how to create a basic PHP login page, feel free to experiment and add your own personal touches to make it fit your website’s design and functionality.

If you want to explore more advanced techniques and features, consider looking into frameworks like Laravel or Symfony, which provide robust authentication systems out of the box.

Happy coding!