-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
192 lines (163 loc) · 5.15 KB
/
functions.php
1
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
session_start();
// connect to database
$db = mysqli_connect('localhost', 'root', '', 'attendance');
// variable declaration
$full_name = "";
$username = "";
$roll_no = "";
$email = "";
$mob_no = "";
$errors = array();
// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
register();
}
// REGISTER USER
function register(){
// call these variables with the global keyword to make them available in function
global $db, $errors, $full_name, $username,$roll_no, $email, $mob_no;
// receive all input values from the form. Call the e() function
// defined below to escape form values
$full_name = e($_POST['full_name']);
$username = e($_POST['username']);
$roll_no = e($_POST['roll_no']);
$email = e($_POST['email']);
$mob_no = e($_POST['mob_no']);
$password_1 = e($_POST['password_1']);
$password_2 = e($_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($full_name)) {
array_push($errors, "fullname is required");
}
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($roll_no)) {
array_push($errors, "roll number is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($mob_no)) {
array_push($errors, "mobile number is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
if (isset($_POST['usertype'])) {
$usertype = e($_POST['usertype']);
//$query = "INSERT INTO `multi_login`(`username`, `email`, `usertype`, `password`)
// VALUES ('$username', '$email', '$user_type', '$password')";
$query = "INSERT INTO `attendance`(`full_name`, `username`, `roll_no`, `email`, `mob_no`, `usertype`, `password`)
VALUES ('$full_name','$username','$roll_no','$email','mob_no', '$usertype','$password')";
mysqli_query($db, $query);
$_SESSION['success'] = "New user successfully created!!";
header('location: login.php');
}
else{
//$query = "INSERT INTO `multi_login`(`username`, `email`, `user_type`, `password`)
// VALUES ('$username', '$email', 'user', '$password')";
$query = "INSERT INTO `attendance`(`full_name`, `username`, `roll_no`, `email`, `mob_no`, `usertype`, `password`)
VALUES ('$full_name','$username','$roll_no','$email','$mob_no', '$usertype','$password')";
mysqli_query($db, $query);
// get id of the created user
$logged_in_user_id = mysqli_insert_id($db);
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
$_SESSION['success'] = "You are now logged in";
header('location: index1.php');
}
}
}
// return user array from their id
function getUserById($id){
global $db;
$query = "SELECT * FROM `attendance` WHERE id=" . $id;
$result = mysqli_query($db, $query);
$user = mysqli_fetch_assoc($result);
return $user;
}
// escape string
function e($val){
global $db;
return mysqli_real_escape_string($db, trim($val));
}
function display_error() {
global $errors;
if (count($errors) > 0){
echo '<div class="error">';
foreach ($errors as $error){
echo $error .'<br>';
}
echo '</div>';
}
}
function isLoggedIn()
{
if (isset($_SESSION['user'])) {
return true;
}else{
return false;
}
}
// log user out if logout button clicked
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['user']);
header("location: login.php");
}
if (isset($_POST['login_btn'])) {
login();
}
// LOGIN USER
function login(){
global $db, $username, $errors;
// grap form values
$username = e($_POST['username']);
$password = e($_POST['password']);
// make sure form is filled properly
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// attempt login if no errors on form
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM `attendance` WHERE username='$username' AND password='$password' LIMIT 1";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) { // user found
// check if user is admin or user
$logged_in_user = mysqli_fetch_assoc($results);
if ($logged_in_user['usertype'] == 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: home.php');
}
else{
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: index1.php');
}
}
else {
array_push($errors, "Wrong username/password combination");
}
}
}
function isAdmin()
{
if (isset($_SESSION['user']) && $_SESSION['user']['usertype'] == 'admin' ) {
return true;
}else{
return false;
}
}
?>