How to create admin login page using PHP?
In this article, we will see how we can create a login page for admin, connected with the database, or whose information to log in to the page is already stored in our database.
Follow the steps to create an admin login page using PHP:
Approach: Make sure you have XAMPP or WAMP installed on your windows machine. In case you’re using Linux OS then install the LAMP server. In this article, we will be using the XAMPP server.
1. Create Database: First, we will create a database named ‘geeksforgeeks‘ (you can give any name to your database). You can also use your existing database or create a new one.
2. Create Table: Create a table named ‘adminlogin’ with 3 columns to store the data.
3. Create Table Structure: The table “adminlogin” contains three fields.
- id – primary key – auto increment
- username – varchar(100)
- password – varchar(100)
The datatype for username and password is varchar. The size can be altered as per the requirement. However, 100 is sufficient, and the datatype for “id” is int and it is a primary key.
A primary key also called a primary keyword is a key in a relational database that is unique for each record. It is a unique identifier, such as a driver’s license number, telephone number (including area code), or vehicle identification number (VIN).
Your table structure should look like this.
Or copy and paste the following code into the SQL panel of your PHPMyAdmin.
DROP TABLE IF EXISTS `adminlogin`; CREATE TABLE IF NOT EXISTS `adminlogin` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(100) NOT NULL, `password` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
To do this from SQL Panel refer to the following screenshot.
4. Insert admin login information: Here, we are inserting two records in our table. You can add as many as you want.
Or copy and paste the following code to insert records into the SQL panel.
INSERT INTO `adminlogin` (`id`, `username`, `password`) VALUES (NULL, 'admin', 'admin'), (NULL, 'admin2', 'admin2');
After inserting the values, the table will look like this.
5. Create a folder that includes the following files: The folder should be in “C:\xampp\htdocs\” (or where your XAMPP is installed). For the WAMP server, it should be in “C:\wamp64\www\” and on Linux “/opt/lampp/htdocs”.
- Filename: index.php
- Filename: connection.php
- Filename: login.css
- Filename: validate.php
- Filename: adminpage.php Add anything that you want to display to the admin page.
6. After completing all the above steps, now follow the steps:
- Run XAMPP server
- Start Apache and MySQL services from XAMPP Panel.
- Type http://localhost/loginPage/ in your browser.
You will get the following login page screen.
If you enter the correct credentials i.e. username and password, then you will be logged in to the “admin.php” page.
else, you get an error pop-up alert.
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Please Login to comment...