-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
113 lines (77 loc) · 2.79 KB
/
login.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
<?php
/// This must come first when we need access to the current session
session_start();;
require("classes/components.php");
/**
* Included for the postValuesAreEmpty() and
* escape() functions and the project file path.
*/
require("classes/utils.php");
/// Redirect user from this page if they're already logged in
if (isset($_SESSION["loggedIn"])) {
header("Location: " . Utils::$projectFilePath . "/pet-list.php");
}
$output = "";
/// Detect if this page has received a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
require("classes/user.php");
/// Determine which form to work with using the submit button's name
if (isset($_POST["loginSubmit"])) {
$output = User::login();
}
if(stripos($output, "error") != true){
$_SESSION["successMessage"] = "Registration Successful!";
header("Location: " . Utils::$projectFilePath . "/success.php");
}
}
Components::pageHeader("Login", ["style"], ["mobile-nav"]);
?>
<main class="content-wrapper login-content">
<h2>Log in to an existing account</h2>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" class="form">
<label>Username</label>
<input
type="text"
name="username"
value="<?php
/**
* If there is an error, then the login form was submitted and the username
* exists, so we can preserve the username in the form.
*
* We need to check if the username is set since the previous form
* submission may have omitted it.
*/
if ($output && isset($_POST["loginSubmit"]) && isset($_POST["username"])) {
echo Utils::escape($_POST["username"]);
}
?>"
>
<label>Password</label>
<input type="password" name="password">
<input class="button" type="submit" onclick="return validateLogin()" name="loginSubmit" value="Log in">
<!-- Only output if there is an error in the registration form -->
<?php if ($output && isset($_POST["loginSubmit"])) { echo $output; } ?>
</form>
<a href="register.php">Register New Account</a>
</main>
<script>
/**
* Validates the login form by checking if the username and password fields are filled out.
*/
function validateLogin() {
let formName = document.forms[0]["username"].value.trim();
let formPassword = document.forms[0]["password"].value.trim();
if (formName === "") {
alert("Username must be filled out");
return false;
}
if (formPassword === "") {
alert("Password must be filled out");
return false;
}
return true;
}
</script>
<?php
Components::pageFooter();
?>