All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Admin Login Page In PHP

Last Updated : Mar 11, 2024

Admin Login Page In PHP

In this article we will show you the solution of admin login page in PHP, here first we need to create login form with some input fields and database connection.

As we know valid users only successfully gets login otherwise it won’t allow user to interact with database because it leads to unauthorized person can access database data easily.

If they are valid user of admin then admin can have priority to see and access database data at his home page.

Step By Step Guide On Admin Login Page In PHP :-

Here we developed login form with two input fields ‘Name, Password’ there admin have to enter login details then we can check whether admin is valid or not by their login details.

If they are entered valid login details then admin navigate to their home page otherwise we can’t move to any other page and at valid admin home page we provided all database data, admin only can view all database data from server.

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="validate.php" method="post">
<h1>Login</h1>
<input type="text" placeholder="Username" name="username" value="">
<input type="password" placeholder="Password" name="password" value="">
<input class="button" type="submit" name="login" value="Sign In">
</form>
</body>
</html>
validate.php
<?php
session_start();
$conn = "";
$conn = mysqli_connect("localhost","root","","dbase");
if (!$conn) {
    echo "Connection failed!";
}
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST["username"];
    $password = $_POST["password"];
    $res=mysqli_query($conn, "SELECT * FROM users");
    $row = mysqli_fetch_assoc($res);
        if(($row['username'] == $username) && ($row['user_type'] == 'admin')) {
            $_SESSION['user']=$row['username'];
                header("location: admin.php");
        }
        else {
            echo "<script language='javascript'>";
            echo "alert('WRONG INFORMATION')";
            echo "</script>";
            die();
        }
    }
?>
Admin.php
<?php
session_start();
$conn = "";
$conn = mysqli_connect("localhost","root","","dbase");
if (!$conn) {
    echo "Connection failed!";
}
?>
<h2>Welcome Admin <?php echo $_SESSION['user'] ?></h2>
<br>
<h3><i>Database Users All Details</i></h3>
<?php
$res=mysqli_query($conn, "SELECT * FROM users");
if(mysqli_num_rows($res) > 0)
{
  echo'<table border=1>
 <tr>
  <th>User Name</th>
  <th>Email</th>
        <th>Password</th>
    </tr>';
    while($r = mysqli_fetch_array($res))
 {
    echo '<tr>
        <td>'.$r["username"].'</td>
        <td>'.$r["email"].'</td>
        <td>'.$r["password"].'</td>
    </tr>';
    }
    echo '</table>';
}
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we developed index page with login form layout there we defined two input fields ‘Name, Password’ user needs to fill their login details.
  4. In login form we need to confirm two things, at input element definition we have to define ‘name’ attributes with respective values ‘username, password’ and at form element need to specify method attribute with ‘post’ value, action attribute with ‘validate.php’ external php file which is used to redirect us to next page.
  5. In validate.php we at first we started session which is used to maintain login admin details for particular amount of time. Here we executed database connection by using ‘mysqli_connect()’ method which contains database, server connection details that is referred by variable ‘$conn’.
  6. If database existed then it return to if condition there we checking whether server requesting post method and then ‘name, password’ input fields collected and stored on respective variables ‘$username, $password’.
  7. Using select query we executing query on database which result stored on variable ‘$res’ by mysqli_query() with the help of database connection $conn. Then mysqli_fetch_assoc() used to collect table ‘users’ details which array of values stored on variable ‘$row’.
  8. Using if condition we checking whether user inputs match with database table data and that user’s user type is ‘admin’ then we storing admin name on session variable and redirect to admin’s home page ‘admin.php’ otherwise it displays error message.
  9. In admin.php we started session and displaying admin name with welcome message then we checking whether row number count by ‘mysqli_num_rows()’ method using if condition.
  10. Within that we defined table with three coulmns ‘User Name,Email, Password’ after header column we needs to use ‘mysqli_fetch_array()’ method for access array of table ‘users’ data using while loop.
  11. Then fetching table ‘users’ data as row by row and displayed on table by specifying database values ‘$r["username"], $r["email"], $r["password"]’ between <td> table coulmns.
  12. Finally confirm one thing is every table tag had their closing tag otherwise we can’t get table structure.

Conclusion :-

In conclusion now we are able to know how to create admin login page in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser we can see login form there admin needs to provide their name and password then it will redirect them to their home page at we can see whole database table users details in table if they provide valid details otherwise it throws error and we can’t move to any other page.

I hope this article on admin login page in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪