multiple user role based login in php and mysql with SESSION - Web Development and Web Design Codes

Latest

Friday, May 26, 2017

multiple user role based login in php and mysql with SESSION

PHP MySQL Multi Role Based Login and Logout System With SESSION (Admin,User and Moderator Panel)

PHP MySQL Multi Role Login

Hi Guys Today we Will learn how to Create a Multi Role SESSION Based Login,Logout System using PHP and MySQL.
You Do not Need To Create Different Login Page for Different User.You Can Process Multiple User Login With Single page.
Ex: After Login admin will Redirect admin page,user will Redirect user Page and Moderator will Redirect Moderator Page.
also learn PHP OOP Login and Registration System
If SESSION Exist User Will Automatically Redirect Their Page.
learn more about PHP SESSION
Also Learn
1. PHP MySQL Pagination
2. PHP MySQL Login with SESSION
3. php mysql comment system
4. sort MySQL table column using PHP
5. PHP MySQL Shopping Cart

Database and Files Creation:

Database Structure:
Database Name: codenair
Table Name: user
Use the below SQL Commands to Create Table Named 'user'
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `role` varchar(100) NOT NULL,
   PRIMARY KEY(ID)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;


Dumping data for table `user`
INSERT INTO `user` (`id`, `username`, `password`, `role`) VALUES
(1, 'RAJ', '12345', 'user'),
(2, 'ABCD', '12345', 'admin'),
(3, 'XYZ', '12345', 'moderator');

File Structure:
1. index.php
2. admin.php
3. user.php
4. moderator.php
5. logout.php

Now Copy The below Codes for Certain Files.
index.php
<?php
session_start();
$conn=mysqli_connect('localhost','root','','codenair');
//Getting Input value
if(isset($_POST['login'])){
  $username=mysqli_real_escape_string($conn,$_POST['username']);
  $password=mysqli_real_escape_string($conn,$_POST['password']);
  if(empty($username)&&empty($password)){
  $error= 'Fileds are Mandatory';
  }else{
 //Checking Login Detail
 $result=mysqli_query($conn,"SELECT*FROM user WHERE username='$username' AND password='$password'");
 $row=mysqli_fetch_assoc($result);
 $count=mysqli_num_rows($result);
 if($count==1){
      $_SESSION['user']=array(
   'username'=>$row['username'],
   'password'=>$row['password'],
   'role'=>$row['role']
   );
   $role=$_SESSION['user']['role'];
   //Redirecting User Based on Role
    switch($role){
  case 'user':
  header('location:user.php');
  break;
  case 'moderator':
  header('location:moderator.php');
  break;
  case 'admin':
  header('location:admin.php');
  break;
 }
 }else{
 $error='Your PassWord or UserName is not Found';
 }
}
}
?>
<html>
<head>
<title>PHP MySQL Role Based Access Control</title>
</head>
<div align="center">
<h3>PHP MySQL Role Based Access Control</h3>
<form method="POST" action="">
<table>
   <tr>
     <td>UserName:</td>
  <td><input type="text" name="username"/></td>
   </tr>
   <tr>
     <td>PassWord:</td>
  <td><input type="text" name="password"/></td>
   </tr>
   <tr>
     <td></td>
  <td><input type="submit" name="login" value="Login"/></td>
   </tr>
</table>
</form>
<?php if(isset($error)){ echo $error; }?>
</div>
</html>
admin.php
<?php
session_start();
//Checking User Logged or Not
if(empty($_SESSION['user'])){
 header('location:index.php');
}
//Restrict User or Moderator to Access Admin.php page
if($_SESSION['user']['role']=='user'){
 header('location:user.php');
}
if($_SESSION['user']['role']=='moderator'){
 header('location:moderator.php');
}
?>
<h1>Wellcome to <?php echo $_SESSION['user']['username'];?> Page</h1>


<link rel="stylesheet" href="style.css" type="text/css"/>
<div id="profile">
<h2>User name is: <?php echo $_SESSION['user']['username'];?> and Your Role is :<?php echo $_SESSION['user']['role'];?></h2>
<div id="logout"><a href="logout.php">Please Click To Logout</a></div>
</div>
user.php
<?php
session_start();
//Checking User Logged or Not
if(empty($_SESSION['user'])){
 header('location:index.php');
}
//Restrict admin or Moderator to Access user.php page
if($_SESSION['user']['role']=='admin'){
 header('location:admin.php');
}
if($_SESSION['user']['role']=='moderator'){
 header('location:moderator.php');
}
?>
<h1>Wellcome to <?php echo $_SESSION['user']['username'];?> Page</h1>


<link rel="stylesheet" href="style.css" type="text/css"/>
<div id="profile">
<h2>User name is: <?php echo $_SESSION['user']['username'];?> and Your Role is :<?php echo $_SESSION['user']['role'];?></h2>
<div id="logout"><a href="logout.php">Please Click To Logout</a></div>
</div>
moderator.php
<?php
session_start();
//Checking User Logged or Not
if(empty($_SESSION['user'])){
 header('location:index.php');
}
//Restrict User or admin to Access moderator.php page
if($_SESSION['user']['role']=='user'){
 header('location:user.php');
}
if($_SESSION['user']['role']=='admin'){
 header('location:admin.php');
}
?>
<h1>Wellcome to <?php echo $_SESSION['user']['username'];?> Page</h1>


<link rel="stylesheet" href="style.css" type="text/css"/>
<div id="profile">
<h2>User name is: <?php echo $_SESSION['user']['username'];?> and Your Role is :<?php echo $_SESSION['user']['role'];?></h2>
<div id="logout"><a href="logout.php">Please Click To Logout</a></div>
</div>
logout.php
Create Logout Page to destory SESSION.
<?php
session_start();
session_destroy();
header('location:index.php');
?>

ok You have Successfully Created Multi Role Based User Login System with SESSION.
Please Stay With Us .Thank You for Your Support.

Tags: role based authentication ,php user authentication ,php mysql login,login script

6 comments:

  1. what of a user with multiple roles?? how do you implement that?

    ReplyDelete
  2. I'm getting a too many redirects error. How to solve?

    ReplyDelete
  3. 'm getting a too many redirects error. How to solve

    ReplyDelete
    Replies
    1. Thank you for Your comment.can you explain me your problem???

      Delete
  4. It would be great if you would do it with MSQLI instead of MSQL
    ThanK You

    ReplyDelete

Thank You for Your Comment

Note: Only a member of this blog may post a comment.