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

Latest

Friday, May 26, 2017

multi user role based login in php with mysql

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

multi user role based login in php with mysql

Hi Guys Today we Will learn how to Create a Muli Role Based Login System using PHP and MySQL.
You Do not Need To Create Different Login Page for Different User.You Can Process Multi 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 mysql role based access control with session

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');

Database Structure:
Create 5 Files in a Folder Named:
1. connect.php
2. index.php
3. admin.html
4. user.html
5. moderator.html
multi user role based login in php with mysql

Now Copy The below Codes for Certain Files.
connect.php
<?php
// Creating connection test is our database name
$conn = new mysqli('localhost', 'root', '','codenair');
// Checking connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

index.php
<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>
</div>
</html>
<?php
include("connect.php");
if(isset($_POST['login'])){
  $name=mysqli_real_escape_string($conn,$_POST['username']);
  $pass=mysqli_real_escape_string($conn,$_POST['password']);
  //RUN Sql query to Get result
$result=mysqli_query($conn,"SELECT*FROM user WHERE username='$name' AND password='$pass'");
$count=$result->num_rows;
//If user Not Found $count will be not 1
if($count !==1){
 echo "Sorry UserName or Password Do Not Match";
}else{
 $row=$result->fetch_array();
 $role=$row['role'];
   switch($role){
    case'admin':
    header('location:admin.html');
    break;
    case'user':
    header('location:user.html');
    break;
    case'moderator':
    header('location:moderator');
    break;
   }
}
}
?>

admin.html
Admin Role User Will Redirect Admin Page.
<h1>Wellcome to <font style="background:red">Admin</font> Page..This is Admin Area</h1>

moderator.html
Moderator Role Will Redirect Moderator Page.
<h1>Wellcome to <font style="background:red">Moderator</font> Page..This is Moderator Area</h1>

user.html
User Role User Will Redirect user Page.
<h1>Wellcome to <font style="background:red">User</font> Page..This is User Area</h1>

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

No comments:

Post a Comment

Thank You for Your Comment

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