Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
79 lines (64 loc) · 1.61 KB
/
Copy pathfunctions.php
File metadata and controls
79 lines (64 loc) · 1.61 KB
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
<?php
session_start();
// connect to database
$db = mysqli_connect('localhost', 'root', '', 'online_bus_booking');
// variable declaration
$username = "";
$email = "";
$errors = array();
function display_error() {
global $errors;
if (count($errors) > 0){
echo '<div class="error">';
foreach ($errors as $error){
echo $error .'<br>';
}
echo '</div>';
}
}
//prevent homepage appearence by url when not logged in
function isLoggedIn()
{
if (isset($_SESSION['user'])) {
return true;
}else{
return false;
}
}
// log user out if logout button clicked
// call the login() function if register_btn is clicked
if (isset($_POST['login_btn'])) {
login();
}
// LOGIN USER
function login(){
global $db, $username, $errors;
// grap form values
$username = ($_POST['user-name']);
$password = ($_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) {
$query = "SELECT * FROM company_personel WHERE username='" . $_POST["user-name"] . "' and password = '". $_POST["Password"]."'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) { // user found
// check if user registerd
$row = mysqli_fetch_assoc($results);
if(is_array($row)){
$_SESSION['userID'] = $row['userID'];
$_SESSION['password'] = $row['password'];
header('location: addbus.php');
}
}
else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>