Part 10-[A]-Admin Panel(Admin Roles): Multi Login / User and Admin Login in PHP
As per the video, we have already downloaded the files and linked the CSS and JS.
Step 1: Create a page named register.php and paste the below:
<?php
include('security.php');
?>
<form action="code.php" method="POST">
<div class="modal-body">
<div class="form-group">
<label> Username </label>
<input type="text" name="username" class="form-control" placeholder="Enter Username">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control checking_email" placeholder="Enter Email">
<small class="error_email" style="color: red;"></small>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Enter Password">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="confirmpassword" class="form-control" placeholder="Confirm Password">
</div>
<input type="hidden" name="usertype" value="admin">
</div>
<div class="modal-footer">
<button type="submit" name="registerbtn" class="btn btn-primary">Save</button>
</div>
</form>
Step 2: Create a page with the name code.php and then goto your phpmyadmin and create one more column in your register table named usertype (Varchar - 50) and paste the below code.
<?php
include('security.php');
if(isset($_POST['registerbtn']))
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
$usertype = $_POST['usertype'];
$email_query = "SELECT * FROM register WHERE email='$email' ";
$email_query_run = mysqli_query($connection, $email_query);
if(mysqli_num_rows($email_query_run) > 0)
{
$_SESSION['status'] = "Email Already Taken. Please Try Another one.";
$_SESSION['status_code'] = "error";
header('Location: register.php');
}
else
{
if($password === $cpassword)
{
$query = "INSERT INTO register (username,email,password,usertype) VALUES ('$username','$email','$password','$usertype')";
$query_run = mysqli_query($connection, $query);
if($query_run)
{
// echo "Saved";
$_SESSION['status'] = "Admin Profile Added";
$_SESSION['status_code'] = "success";
header('Location: register.php');
}
else
{
$_SESSION['status'] = "Admin Profile Not Added";
$_SESSION['status_code'] = "error";
header('Location: register.php');
}
}
else
{
$_SESSION['status'] = "Password and Confirm Password Does Not Match";
$_SESSION['status_code'] = "warning";
header('Location: register.php');
}
}
}
?>
Step 3: Your login.php will be same as it before, form given below:
<form class="user" action="code.php" method="POST">
<div class="form-group">
<input type="email" name="emaill" class="form-control form-control-user" placeholder="Enter Email Address...">
</div>
<div class="form-group">
<input type="password" name="passwordd" class="form-control form-control-user" placeholder="Password">
</div>
<button type="submit" name="login_btn" class="btn btn-primary btn-user btn-block"> Login </button>
<hr>
</form>
Step 4: Get back to your code.php file and paste the below code:
<?php
include('security.php');
if(isset($_POST['login_btn']))
{
$email_login = $_POST['emaill'];
$password_login = $_POST['passwordd'];
$query = "SELECT * FROM register WHERE email='$email_login' AND password='$password_login' LIMIT 1";
$query_run = mysqli_query($connection, $query);
$usertypes = mysqli_fetch_array($query_run);
if($usertypes['usertype'] == "admin")
{
$_SESSION['username'] = $email_login;
header('Location: index.php');
}
else if($usertypes['usertype'] == "user")
{
$_SESSION['cusername'] = $email_login;
header('Location: ../index.php');
}
else
{
$_SESSION['status'] = "Email / Password is Invalid";
header('Location: login.php');
}
}
?>
Step 5: Create a folder called database and then create a file named dbconfig.php and paste the below code.
<?php
$server_name = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "adminpanel";
$connection = mysqli_connect($server_name,$db_username,$db_password,$db_name);
if(!$connection)
{
die("Connection failed: " . mysqli_connect_error());
echo '
<div class="container">
<div class="row">
<div class="col-md-8 mr-auto ml-auto text-center py-5 mt-5">
<div class="card">
<div class="card-body">
<h1 class="card-title bg-danger text-white"> Database Connection Failed </h1>
<h2 class="card-title"> Database Failure</h2>
<p class="card-text"> Please Check Your Database Connection.</p>
<a href="#" class="btn btn-primary">:( </a>
</div>
</div>
</div>
</div>
</div>
';
}
?>
Step 6: Create a page security.php and paste the below code.
<?php
session_start();
include('database/dbconfig.php');
if($connection)
{
// echo "Database Connected";
}
else
{
header("Location: database/dbconfig.php");
}
if(!$_SESSION['username'])
{
header('Location: login.php');
}
?>