Skip to content

Commit 46e42fe

Browse files
authored
Create README.md
1 parent fbff8d7 commit 46e42fe

File tree

1 file changed

+330
-0
lines changed

1 file changed

+330
-0
lines changed

README.md

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
# PHP AJAX LOGIN With Different User Level
2+
3+
This Application is the basic and simple PHP Ajax Login with different User Role or level with active and inactive user account,
4+
this example is included with the user level of Superuser Module / System admin Module / Hr Module / and The normal user which is
5+
the employee module. You will free to modify the code and add your own Security features like.
6+
7+
- Secure login
8+
- CSRF protection
9+
- Bruteforce protection
10+
- login attemps
11+
- Secure password reset
12+
- and etch
13+
14+
15+
#### Folder Structures
16+
```html
17+
- assets/
18+
├── custom.css
19+
- database/
20+
├── hris.sql - import this file
21+
- includes/
22+
├── menu-for-hr.php
23+
├── menu-for-superuser.php
24+
├── menu-for-sysadmin.php
25+
├── menu-for-user.php
26+
- library
27+
├── db.php - db connection
28+
├── functions.php - optional you can write your oop concept here or its up to you
29+
- login
30+
├── assets/
31+
├── bootstrap/
32+
├── js/
33+
├── login.js - your login js
34+
├── index.php - login view
35+
├── logout.php - your session destroy
36+
├── login.php - login script
37+
- template/
38+
├── header.php - your html header
39+
├── content.php - include the header and content
40+
├── footer.php - your footer header
41+
- views /
42+
├── hr/
43+
├── index.php
44+
├── superuser/
45+
├── index.php
46+
├── sysadmin/
47+
├── index.php
48+
├── user/
49+
├── index.php
50+
- index.php - this will be your route logic
51+
52+
```
53+
54+
55+
56+
#### db.php
57+
Create database or import the database included from files then create a connection using PDO.
58+
59+
```php
60+
<?php session_start() ?>
61+
<?php
62+
63+
$ServerName = "localhost";
64+
$Username = "root";
65+
$Password = "";
66+
$DbName = "hris";
67+
$db = null;
68+
69+
try {
70+
$db = new PDO("mysql:host=$ServerName;dbname=$DbName",$Username,$Password);
71+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
72+
73+
} catch (PDOException $e) {
74+
echo "Connection Failed:" . $e->getMessage();
75+
die();
76+
}
77+
78+
?>
79+
<?php require 'functions.php' ?>
80+
```
81+
82+
83+
#### index.php
84+
This will be your route logic but since this is not a full mvc concept but we will called this a route :-)
85+
86+
```php
87+
<?php
88+
if (!isset($_SESSION['user']['IsLoggeD']) && $_SESSION['user']['IsLoggeD'] != true) {header("Location:login");}
89+
$pg = (isset($_GET['pg']) && !empty($_GET['pg']) ? $_GET['pg']:'');
90+
$userLevel = $_SESSION['user']['userLevel'];
91+
if($userLevel == 'SUPERUSER'){
92+
switch ($pg) {
93+
case 'dashboard':
94+
$title = 'dashboard';
95+
$content = 'views/superuser/index.php';
96+
$active = 'dashboard';
97+
$css = array(""); // you can include here your css style in your per module but depends on you
98+
$js = array(""); // your action.js every module
99+
$flag = 1; // if 1 has include files else no include files
100+
break;
101+
102+
103+
default:
104+
$title = 'dashboard';
105+
$content = 'views/superuser/index.php';
106+
$active = 'dashboard';
107+
$css = array(""); // you can include here your css style in your per module but depends on you
108+
$js = array(""); // your action.js every module
109+
$flag = 1; // if 1 has include files else no include files
110+
break;
111+
}
112+
}elseif ($userLevel == 'SYSADMIN') {
113+
switch ($pg) {
114+
case 'dashboard':
115+
$title = 'dashboard';
116+
$content = 'views/sysadmin/index.php';
117+
$active = 'dashboard';
118+
$css = array(""); // you can include here your css style in your per module but depends on you
119+
$js = array(""); // your action.js every module
120+
$flag = 1; // if 1 has include files else no include files
121+
break;
122+
123+
default:
124+
$title = 'dashboard';
125+
$content = 'views/sysadmin/index.php';
126+
$active = 'dashboard';
127+
$css = array(""); // you can include here your css style in your per module but depends on you
128+
$js = array(""); // your action.js every module
129+
$flag = 1; // if 1 has include files else no include files
130+
break;
131+
}
132+
}elseif ($userLevel == 'HR') {
133+
switch ($pg) {
134+
case 'dashboard':
135+
$title = 'dashboard';
136+
$content = 'views/hr/index.php';
137+
$active = 'dashboard';
138+
$css = array(""); // you can include here your css style in your per module but depends on you
139+
$js = array(""); // your action.js every module
140+
$flag = 1; // if 1 has include files else no include files
141+
break;
142+
143+
default:
144+
$title = 'dashboard';
145+
$content = 'views/hr/index.php';
146+
$active = 'dashboard';
147+
$css = array(""); // you can include here your css style in your per module but depends on you
148+
$js = array(""); // your action.js every module
149+
$flag = 1; // if 1 has include files else no include files
150+
break;
151+
}
152+
}elseif ($userLevel == 'USER') {
153+
switch ($pg) {
154+
case 'dashboarduser':
155+
$title = 'dashboard';
156+
$content = 'views/user/index.php';
157+
$active = 'dashboard';
158+
$css = array(""); // you can include here your css style in your per module but depends on you
159+
$js = array(""); // your action.js every module
160+
$flag = 1; // if 1 has include files else no include files
161+
break;
162+
163+
default:
164+
$title = 'dashboard';
165+
$content = 'views/user/index.php';
166+
$active = 'dashboard';
167+
$css = array(""); // you can include here your css style in your per module but depends on you
168+
$js = array(""); // your action.js every module
169+
$flag = 1; // if 1 has include files else no include files
170+
break;
171+
}
172+
173+
}
174+
175+
?>
176+
```
177+
178+
179+
#### login folder
180+
in login folder you will see the login index, login.php and logout since is not a mvc concept we have separated folder called login in login folder we have index.php
181+
logout.php and login.php this are your login sript you can modify or make oop class to make it one file only.
182+
183+
184+
#### login.php
185+
```php
186+
<?php require'../library/db.php' ?>
187+
<?php extract($_POST) ?>
188+
189+
<?php
190+
$sql = $db->query("SELECT * FROM tbl_users WHERE userName = '".$username."' AND userPass = '".$password."' ");
191+
$row = $sql->fetch(PDO::FETCH_ASSOC);
192+
193+
if($sql->rowCount() > 0){
194+
if($row['userStatus'] == 1){
195+
$_SESSION['user'] = array(
196+
'userLevel' => $row['userType'],
197+
'fullname' => $row['userName'],
198+
'IsLoggeD' => true
199+
);
200+
201+
$response = array("response" => "Success",
202+
"User" => $row['userType']);
203+
204+
}else{
205+
$response = array("response" => "Lock","Message" => "Your Account is Temporarily Locked");
206+
}
207+
208+
}else{
209+
210+
$response = array("response" => "Invalid",
211+
"message" => "Invalid Password");
212+
}
213+
214+
echo json_encode($response);
215+
?>
216+
```
217+
218+
#### logout.php
219+
```php
220+
<?php require '../library/db.php' ?>
221+
222+
<?php
223+
224+
session_destroy();
225+
unset($_SESSION);
226+
header("Location:index.php");
227+
?>
228+
```
229+
230+
### login.js
231+
in assets/js we will find the login.js this is our jquery ajax script with json response
232+
233+
```javascript
234+
$(document).ready(function(){
235+
$("#username").on("keyup",function(){
236+
$("#msg").hide();
237+
});
238+
239+
$("#password").on("keyup",function(){
240+
$("#msg").hide();
241+
});
242+
243+
function loader(){
244+
$("#msg").html('<br/><br/><br/><br/><img src="ajax-loader.gif" width="40px" height="40px;"/> &nbsp;<font size="6">Welcome...</font><br/><br/>');
245+
}
246+
247+
function hideField(){
248+
$("#username").hide();
249+
$("#password").hide();
250+
$("#uname").hide();
251+
$("#upass").hide();
252+
$("#login_button").hide();
253+
$("#question").hide();
254+
$("#msg").hide();
255+
}
256+
257+
258+
$("#loginForm").on("submit",function(){
259+
var loginData = $(this).serialize();
260+
$.ajax({
261+
type:'POST',
262+
url:'login.php',
263+
dataType:'json',
264+
data: loginData,
265+
beforeSend:function(data){
266+
267+
},
268+
success:function(data){
269+
if(data.response == "Success"){
270+
if(data.User == "SUPERUSER")
271+
{
272+
alert("SUPERUSER");
273+
hideField();
274+
$("#msg").fadeIn(function(){loader()});
275+
setTimeout(' window.location.href = "../?pg=superuser"; ',5000);
276+
}
277+
278+
else if(data.User =="SYSADMIN")
279+
{
280+
alert("SYSADMIN");
281+
hideField();
282+
$("#msg").fadeIn(function(){loader()});
283+
setTimeout(' window.location.href = "../?pg=sysadmin"; ',5000);
284+
}
285+
286+
else if(data.User =="HR")
287+
{
288+
alert("HR");
289+
hideField();
290+
$("#msg").fadeIn(function(){loader()});
291+
setTimeout(' window.location.href = "../?pg=hr"; ',5000);
292+
}
293+
294+
else if(data.User =="USER")
295+
{
296+
alert("USER");
297+
hideField();
298+
$("#msg").fadeIn(function(){loader()});
299+
setTimeout(' window.location.href = "../?pg=user"; ',5000);
300+
}
301+
302+
} else if (data.response == "Lock") {
303+
$("#msg").fadeIn(1000, function(){
304+
$("#msg").html('<div class="alert alert-danger"> <span class="fa fa-warning"></span> &nbsp; Your account is Temporarily Lock please contact &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspAdministrator for this Assistant !</div>');
305+
});
306+
} else{
307+
$("#msg").fadeIn(1000, function(){
308+
$("#msg").html('<div class="alert alert-danger"> <span class="fa fa-warning"></span> &nbsp; Usrname or Password is Invalid !</div>');
309+
});
310+
}
311+
},
312+
313+
error:function(xhr,status){
314+
alert(status.error);
315+
}
316+
});
317+
return false;
318+
});
319+
320+
321+
});
322+
```
323+
324+
325+
### just download to checkout the full details https://github.com/reyven90/php-ajax-login
326+
327+
### more question just pm me on facebook https://www.facebook.com/jay.romantiko
328+
329+
### thank you for following Goodbless :-)
330+

0 commit comments

Comments
 (0)