Creating Multi User Role Based Admin Using PHP Mysql and Bootstrap - Thesoftwareguy
Creating Multi User Role Based Admin Using PHP Mysql and Bootstrap - Thesoftwareguy
TRENDING
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
35 Important interview questions with answers for php freshers
Home
PHP
Search...
Creating multi user role based admin using php mysql and bootstrap
Creating multi user role based admin using php mysql and
bootstrap
40
ABOUT ME
Shahrukh Khan
PHP
IN YOUR
INBOX
Subscribe to our mailing list and get interesting
stuff and updates to your email inbox.
Last two weeks I was quite busy with projects and hardly had any spare time left for writing blogs. I
had a huge backlog of mails requesting for tutorials. One thing I found common among them was
creating a multi user role based admin feature. I googled for the article so I can give them links but I
was not able to find useful tutorial. So i decided to make it myself for my readers. In this tutorial I will
be Creating multi user role based admin using php mysql and bootstrap library.
SIGN UP NOW
we respect your privacy and take protecting it
seriously
View Demo
Home is multi
PHP user
Jquery
What
role
based Snippet
admin?
Ajax
Projects
Demos
Contact Me
For novice users let me explain what this article is all about. Suppose you have an online inventory store. You
have multiple employee each has their specific roles. i.e some person are responsible for feeding data (Data
Operator), some are responsible for customer support and some for sales. In this case you dont want all your
modules/data to be available to every one of them. So what you have to do is to assign a role to them, and
then they will have the privilege to access limited data only.
In this tutorial I am not going to make a full fledged admin panel. I will show the trick using mysql database and
php logic to create multi user admin. Follow the steps below.
Step 1. Create a database and add modules,system users, role and their rights.
The first step is to create a database. I have created a database named multi-admin. Create some modules
that you will be using in your application. Check the sample sql below.
2
3
4
5
6
7
8
9
10
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
1/11
11/28/2015
11
12
13
14
15
16
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
Once you have created modules table, feed some data into it. I have used purchases, sales, stocks and
Shipping, payment and taxes. So there are 6 modules in two groups.
2
3 INSERT INTO module (mod_modulegroupcode, mod_modulegroupname, mod_modulecode, mod_modulename,
mod_modulegrouporder, mod_moduleorder, mod_modulepagename) VALUES
4 ("INVT","Inventory", "PURCHASES","Purchases", 2, 1,'purchases.php'),
5 ("INVT","Inventory", "STOCKS","Stocks", 2, 2,'stocks.php'),
6 ("INVT","Inventory", "SALES","Sales", 2, 3,'sales.php'),
7 ("CHECKOUT","Checkout","SHIPPING","Shipping", 3, 1,'shipping.php'),
8 ("CHECKOUT","Checkout","PAYMENT","Payment", 3, 2,'payment.php'),
9 ("CHECKOUT","Checkout","TAX","Tax", 3, 3,'tax.php')
Add system user/admin who will manage the application. Assign each admin with a role.
2
3
4
5
6
7
8
9
10
11
12
13
14
The final step is to give each role the privilege to access modules. I have used 4 options i.e create, edit, view
and delete.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
POPULAR POST
RECENT POST
78
56
46
41
This step is very easy. You have to create files for each modules based on names you have given in the
database (module table). Apart from the 6 pages that are given the database, you have to create 3 more pages
viz. login.php (user will login), dashboard.php (user will see the menu/modules), and logout.php (to clear the
LIKE US ON FACEBOOK
session).
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
2/11
11/28/2015
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="user_password"><span class="r
equired">*</span>Password:</label>
<div class="col-lg-6">
<input type="password" value="" placeholder="Password" id="user_pass
word" class="form-control" name="user_password" required="" >
</div>
</div>
<div class="form-group">
<div class="col-lg-6 col-lg-offset-2">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</fieldset>
</form>
define('DB_DRIVER', 'mysql')
define('DB_SERVER', 'localhost')
define('DB_SERVER_USERNAME', 'root')
define('DB_SERVER_PASSWORD', '')
define('DB_DATABASE', 'multi-admin')
define('PROJECT_NAME', 'Create Multi admin using php mysql and bootstrap library')
$dboptions = array(
PDO::ATTR_PERSISTENT => FALSE,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
)
try {
$DB = new PDO(DB_DRIVER.':host='.DB_SERVER.'dbname='.DB_DATABASE, DB_SERVER_USERNAME, D
B_SERVER_PASSWORD , $dboptions)
} catch (Exception $ex) {
echo $ex->getMessage()
die
}
require_once 'functions.php'
CATEGORIES
Achievements
(4)
(8)
Interview
(1)
Jquery
(22)
(11)
Mini Projects
(5)
MySQL Snippet
(4)
PHP
(41)
PHP Snippet
(24)
PHP Tutorial
(6)
Premium Projects
(4)
(9)
FOLLOW US ON TWITTER
Follow@thesoftwareguy7
124followers
$mode = $_REQUEST["mode"]
if ($mode == "login") {
$username = trim($_POST['username'])
$pass = trim($_POST['user_password'])
$_SESSION["errorType"] = "danger"
$_SESSION["errorMsg"] = "Enter manadatory fields"
} else {
$sql = "SELECT * FROM system_users WHERE u_username = :uname AND u_password = :upass
"
try {
$stmt = $DB->prepare($sql)
// execute Query
$stmt->execute()
$results = $stmt->fetchAll()
if (count($results) > 0) {
$_SESSION["errorType"] = "success"
$_SESSION["errorMsg"] = "You have successfully logged in."
$_SESSION["user_id"] = $results[0]["u_userid"]
$_SESSION["rolecode"] = $results[0]["u_rolecode"]
$_SESSION["username"] = $results[0]["u_username"]
redirect("dashboard.php")
exit
} else {
$_SESSION["errorType"] = "info"
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
thesoftwareguy7
Follow
+1
+ 4,052
3/11
11/28/2015
38
39
40
41
42
43
44
45
46
47
48
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
$_SESSION["errorType"] = "danger"
$_SESSION["errorMsg"] = $ex->getMessage()
}
}
// redirect function is found in functions.php page
redirect("index.php")
}
Once you are logged in you are redirected to dashboard.php where you will see the menu/modules that are
assigned as per your role. Your role is saved in session when you are logged in.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// if the rights are not set then add them in the current session
if (!isset($_SESSION["access"])) {
try {
$stmt = $DB->prepare($sql)
$stmt->execute()
// modules group
$commonModules = $stmt->fetchAll()
$stmt = $DB->prepare($sql)
$stmt->execute()
// all modules
$allModules = $stmt->fetchAll()
$stmt = $DB->prepare($sql)
$stmt->bindValue(":rc", $_SESSION["rolecode"])
$stmt->execute()
// modules based on user role
$userRights = $stmt->fetchAll()
echo $ex->getMessage()
}
}
In the above script all the data are passed into a function named set_rights() which return an array based on
user roles.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$row = array()
for ($j = 0, $c2 = count($menuRights) $j < $c2 $j++) {
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
if (authorize($menuRights[$j]["rr_create"]) || authorize($menuRights[$j]["r
r_edit"]) ||
authorize($menuRights[$j]["rr_delete"]) || authorize($menuRights[$j]
["rr_view"])
) {
$row["menu"] = $menus[$i]["mod_modulegroupcode"]
$row["menu_name"] = $menus[$i]["mod_modulename"]
$row["page_name"] = $menus[$i]["mod_modulepagename"]
$row["create"] = $menuRights[$j]["rr_create"]
$row["edit"] = $menuRights[$j]["rr_edit"]
$row["delete"] = $menuRights[$j]["rr_delete"]
$row["view"] = $menuRights[$j]["rr_view"]
$data[$menus[$i]["mod_modulegroupcode"]][$menuRights[$j]["rr_modulecod
e"]] = $row
$data[$menus[$i]["mod_modulegroupcode"]]["top_menu_name"] = $menus[$i]
["mod_modulegroupname"]
}
}
}
}
return $data
}
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
4/11
11/28/2015
32
33
34
35
36
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
Once you have all the modules based on your role in a session variable. Display it as list menu.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<ul>
<?php foreach ($_SESSION["access"] as $key => $access) { ?>
<li>
<?php echo $access["top_menu_name"] ?>
<?php
echo '<ul>'
foreach ($access as $k => $val) {
if ($k != "top_menu_name") {
echo '<li><a href="' . ($val["page_name"]) . '">' . $val["me
nu_name"] . '</a></li>'
?>
<?php
}
}
echo '</ul>'
?>
</li>
<?php
}
?>
</ul>
You can also add another layer ofsecurity check for each modules pages if you want. In case if user is trying to
access a modules using direct page URL but is not assigned for, they must not passed this security check.
2
3
4
5
6
7
8
9
10
11
12
$status = FALSE
if ( authorize($_SESSION["access"]["INVT"]["PURCHASES"]["create"]) ||
authorize($_SESSION["access"]["INVT"]["PURCHASES"]["edit"]) ||
authorize($_SESSION["access"]["INVT"]["PURCHASES"]["view"]) ||
authorize($_SESSION["access"]["INVT"]["PURCHASES"]["delete"]) ) {
$status = TRUE
}
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
5/11
11/28/2015
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
13 }
session_start()
$_SESSION = array()
unset($_SESSION)
session_destroy()
header("location:index.php")
exit
View Demo
Thedownloadlinkislocked!
Wedon'tneedmoneyfromyou,justuseoneofthebuttonsbelowtoappreciateourworkand
unlockthecontent.
Tweet tweet
Like
5.4k
likeus
PREVIOUS ARTICLE
4k+1us
NEXT ARTICLE
ABOUT AUTHOR
Shahrukh Khan
I am a passionate Software Professional, love to learn and share my knowledge with others.
Software is the hardware of my life.
RELATED POSTS
19
40 COMMENTS
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
6/11
11/28/2015
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
REPLY
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
7/11
11/28/2015
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
REPLY
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
8/11
11/28/2015
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
Thanks in advance.
REPLY
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
9/11
11/28/2015
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
everything is already explained, what part are you facing problem.
REPLY
LEAVE A REPLY
Your Name
Your Email
Your Website
Your Comment
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
10/11
11/28/2015
Creatingmultiuserrolebasedadminusingphpmysqlandbootstrapthesoftwareguy
Post Comment
Notify me of followup comments via e-mail. You can also subscribe without commenting.
http://www.thesoftwareguy.in/creatingmultiuserrolebasedadminusingphpmysqlbootstrap/#prettyPhoto
11/11