How to create admin login page using PHP?
We have created a login page of the admin panel, connected with the database, or whose information to log in to the page is already stored in our database.
Step 1: Creating the Database Table
//Table structure for tablelogin
CREATE TABLElogin
(id
int(11) NOT NULL,username
varchar(255) NOT NULL,password
varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Login page database Structure shows the database.
You insert one record in the database.
Step 2: Creating the Config File
Let’s create a file named “connect.php” and put the following code inside it.
$connection = new mysqli("localhost","User Name","","Database Name"); // Check connection if ($connection -> connect_errno) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error; exit(); }
Step 3: Creating the login File
Let’s create a file named “login.php” and put the following code inside it.
PHP CODE
<?php
session_start();
require_once('connect.php');
$drp_result = mysqli_query($connection,"SELECT * FROM login WHERE id ='1'");
$row = mysqli_fetch_array($drp_result);
if(isset($_POST) & !empty($_POST)){
$username = $_POST['username'];
$password = md5($_POST['password']);
$sql = "SELECT * FROM `login` WHERE username='$username' AND password='$password'"; $result = mysqli_query($connection, $sql); $count = mysqli_num_rows($result); if($count == 1){ $_SESSION['username'] = $username; }else{ $fmsg = "Invalid Username/Password"; }
} if(isset($_SESSION['username'])){ $smsg = "User already logged in"; echo "window.location.href='index.php'; "; exit; } ?>
HTML CODE
<div class="card-body p-0">
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<div class="p-5">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1>
</div>
<form class="user" method="POST">
<div class="form-group">
<input type="text" class="form-control form-control-user" id="username" name="username" aria-describedby="emailHelp" placeholder="User Name">
</div>
<div class="form-group">
<input type="password" class="form-control form-control-user" name="password" id="password" placeholder="Password">
</div>
<div class="form-group form-row mt-3 mb-0">
<button class="btn btn-primary btn-user btn-block" type="submit">Login</button>
</div>
</form>
</div>
</div>
Step 4: Creating the index File
Let’s create a file named “index.php” and put the following code inside it.
<h2>Welcome To Admin</h2>
The output of the above example (i.e. login form) will look something like this:
Author: Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.
Trending Posts
Tags
ACF
Ading custom columns
admin
Bootstrap
Cart Price
categories
Change Price
class
Code
comments
Create
Creating custom post Type
CSS
current
custom
Customizing
Custom Post
Custom Post Type
Custom Taxonomy
database
Date
Fatch
form
function
HTML
Image
Install
javascript
jQuery
Menu
PHP
Post
Post Type
Price
product
responsive
Resular price
Single product
Table
taxonomy
Theme
Title
woocommerce
Wordpress
Wordpress Code