Creating a Simple User Login With IF-ELSE

Conditional statements in PHP allows you to match two or more values. Hence, it is also easier to match username and password with the ones you have stored.

img

If you are looking for a handy, simple, and easy code to create password protected pages without have to use database, here is how.

1. Single User Example

First, we will work with a single username and password. For this, we will create a simple html form with username and password fields.

<html>
<head>
    <title>Login</title>
</head>
<body>

<h1>Login!</h1>

<form action="login.php" method="post">
    <p>Username: <input type="text" name="username"></p>
    <p>Password: <input type="password" name="password"></p>
    <p><input type="submit" name="submit" value="Login"></p>
</form>

</body>
</html>

The above html code has a form, which is posting data to login.php. We used username and password as field names. The login.php will look like this:

<?php

$user = "me";
$pass = "itsme";

if(isset($_POST["submit"])) {
    if($_POST["username"] == $user && $_POST["password"] == $pass) {
        echo "Login successful";
    } else {
        echo "Incorrect Login";
    }
}

?>

In the above example, we defined $user as "me" and $pass as "itsme". First, we will check the any data was sent via POST to login.php. Use isset() function along with if(), and $_POST array with "submit" key (The submit button). If it exist, we compare the sent username and password with $user and $pass respectively.

If the use enters correct username and password, a success login message will be shown. You can replace this with your own html or php code to allow restricted content to users with correct username and password.

2. Multiple Users Example

The login form will stay the same. You will need this code in the login.php:

<?php

$user1 = "John";
$pass1 = "john123";

$user2 = "Ali";
$pass2 = "ali123";

if(isset($_POST["submit"])) {
    if(
        ($_POST["username"] == $user1 && $_POST["password"] == $pass1) ||
        ($_POST["username"] == $user2 && $_POST["password"] == $pass2)
        ) {
        echo "Login successful. Welcome $_POST[username]!";
    } else {
        echo "Incorrect Login";
    }
}

?>

In this example, you can see the we created 2 users with $user1 and $user2 variables along with their passwords similar way.

This time we will need to match each user manually with OR ( || ) operator. The condition tests if this is user1 with pass1, OR the user2 with pass2. If any login combination is accepted, it shows the protected area.

Notice the $_POST["username"] (use quotation marks if you are not working within one already), it outputs the username after successful login, allowing you to customize the message for each user.

Also be careful with capital and small letters. The == operator is case sensitive. The user1 is "John" with Capital J. Small j will not work here.

3. Advanced Multi User Login System

The above example was easy enough, but it would have been difficult if you are willing to create many users. There is a simple way of doing this as well through Array, by using username as keys.

See the code below for login.php:

<?php

$users["one"] = "pass1";
$users["two"] = "pass2";
$users["three"] = "pass3";

if(isset($_POST["submit"])) {

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

    if(isset($users[$username])) {
        if($users[$username] == $password) {
            echo "Login Successful! Welcome $username!";
        } else {
            echo "Incorrect Login";
        }
    } else {
        echo "Username does not exist.";
    }
}

?>

In above code, we created $users array, with keys as username, assigning password to each as an element.

We used if() with isset() first to make sure the data was actually sent.

Then for ease of use, we stored the username and password sent from the form to $username and $password.

Then, we used if() with isset() on $users with its key as $username to see if the username exist in our array.

Finally, we used if() with the $users array variable, with its key as $usename (posted by form) to compare it with the posted $password. Since we know the the value of $username key is the password, it should match it.

The user list can easily be expanded by adding more indexes.

SourceCode

login-form.php

<html>
<head>
    <title>Login</title>
</head>
<body>

<h1>Login!</h1>

<form action="login.php" method="post">
    <p>Username: <input type="text" name="username"></p>
    <p>Password: <input type="password" name="password"></p>
    <p><input type="submit" name="submit" value="Login"></p>
</form>

</body>
</html>

login.php

<?php

/*
Topic: Creating a Simple User Login System with IF-ELSE

Tavistock and Sarfraz API
https://www.tutorialspoint.com/php/php_login_example.htm
*/

//single user
/*
$user = "me";
$pass = "itsme";

if(isset($_POST["submit"])) {
    if($_POST["username"] == $user && $_POST["password"] == $pass) {
        echo "Login successful";
    } else {
        echo "Incorrect Login";
    }
}
*/


//Multple Users 1, case sensitive
/*
$user1 = "John";
$pass1 = "john123";

$user2 = "Ali";
$pass2 = "ali123";

if(isset($_POST["submit"])) {
    if(
        ($_POST["username"] == $user1 && $_POST["password"] == $pass1) ||
        ($_POST["username"] == $user2 && $_POST["password"] == $pass2)
        ) {
        echo "Login successful. Welcome $_POST[username]!";
    } else {
        echo "Incorrect Login";
    }
}
*/


//Multiple Users 2

$users["one"] = "pass1";
$users["two"] = "pass2";
$users["three"] = "pass3";

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

if(isset($_POST["submit"])) {
    if(isset($users[$username])) {
        if($users[$username] == $password) {
            echo "Login Successful! Welcome $username!";
        } else {
            echo "Incorrect Login";
        }
    } else {
        echo "Username does not exist.";
    }
}

?>

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?