Admin Login Page In PHP
Last Updated : Jan 1, 2023
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.
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- Here we developed index page with login form layout there we defined two input fields ‘Name, Password’ user needs to fill their login details.
- 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.
- 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’.
- 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’.
- 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’.
- 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.
- 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.
- 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.
- 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.
- Finally confirm one thing is every table tag had their closing tag otherwise we can’t get table structure.
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.