-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
208 lines (190 loc) · 9.39 KB
/
index.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/** https://cs4640.cs.virginia.edu/cjb2utf/sprint4
* https://cs4640.cs.virginia.edu/rjw3kk/sprint4
* **/
/** DATABASE SETUP **/
include 'database_variables.php';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Extra Error Printing
$mysqli = new mysqli($SERVER, $USERNAME, $PASSWORD, $DATABASE);
//$mysqli = new mysqli("localhost", "root", "", "example"); // XAMPP
$error_msg = "";
session_start();
setcookie("thumbs", json_encode([0]), time() + 3600);
setcookie("industry", "All", time() + 3600);
// Check if the user submitted the form (the form in the HTML below
// submits back to this page, which is okay for now. We will check for
// form data and determine whether to re-show this form with a message
// or to redirect the user to the trivia game.
if (isset($_POST["name"]) && isset($_POST["username"]) && isset($_POST["email"])) { // validate the email coming in
$stmt = $mysqli->prepare("select * from user where username = ?;");
$stmt->bind_param("s", $_POST["username"]);
if (!$stmt->execute()) {
$error_msg = "Error checking for user";
} else {
// result succeeded
$res = $stmt->get_result();
$data = $res->fetch_all(MYSQLI_ASSOC);
if (!empty($data)) {
// user was found!
// validate the user's password
if (password_verify($_POST["password"], $data[0]["password"])) {
// Save user information into the session to use later
$_SESSION["name"] = $data[0]["name"];
$_SESSION["username"] = $data[0]["username"];
$_SESSION["email"] = $data[0]["email"];
if ($data[0]["thumbs"] != null) {
setcookie("thumbs", $data[0]["thumbs"], time() + 3600);
}
header("Location: home.php");
exit();
} else {
// User was found but entered an invalid password
$error_msg = "Invalid Password";
}
} else {
// User was not found. For our game, we'll just insert them!
// NEVER store passwords into the database, use a secure hash instead:
$hash = password_hash($_POST["password"], PASSWORD_DEFAULT);
$insert = $mysqli->prepare("insert into user (name, username, email, password) values (?,?,?,?);");
$insert->bind_param("ssss", $_POST["name"], $_POST["username"], $_POST["email"], $hash);
if (!$insert->execute()) {
$error_msg = "Error creating new user";
}
// Send them to the game
// Save user information into the session to use later
$_SESSION["name"] = $_POST["name"];
$_SESSION["username"] = $_POST["username"];
$_SESSION["email"] = $_POST["email"];
header("Location: home.php");
exit();
}
}
}
?>
<!DOCTYPE html>
<!--
Sources used: https://cs4640.cs.virginia.edu, https: //getbootstrap.com/docs/,
https://www.php.net/docs.php, https: //stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array/2449093,
https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event , https://stackoverflow.com/questions/9334636/how-to-create-a-dialog-with-yes-and-no-options
-->
<html lang="en">
<head>
<link rel="stylesheet" href="styles.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Rena Wolinsky and Colin Buyck">
<meta name="description" content="Login page for website where users can search for climate footprints of companies to educate and empower consumers">
<meta name="keywords" content="Climate, CO2, Footprint, green washing, companies, sustainability, consumerism ">
<title>CEV</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
</head>
<body onload = "autofillOption()">
<!-- Navigation bar code -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark" aria-label="Main Navigation Bar">
<div class="container-xl">
<a class="navbar-brand" href="home.php"><img src="cevLogo.png" width="30" height="30" class="d-inline-block align-top" alt="cev Logo"> Corporate Emissions Viewer</a>
</div>
</div>
</nav>
<div class="container" style="margin-top: 15px;">
<div class="row col-xs-8">
<h2 class="text-center">Sign-In</h2>
<p class = "text-center">Are you ready to learn more about the environmental impact of corporations? To get started, login below or enter a new username, name, and password to create an account.</p>
</div>
<div class="row justify-content-center">
<div class="col-4">
<?php
if (!empty($error_msg)) {
echo "<div class='alert alert-danger'>$error_msg</div>";
}
?>
<div class="text-center">
<button id="autofill" onclick="formFill()" class="btn btn-secondary" disabled> No autofill info </button>
</div>
<form action="index.php" method="post" onsubmit="return validate();">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" maxlength = "30"/>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" />
</div>
<div class="mb-3">
<label for="name" class="form-label" maxlength = "30">Name</label>
<input type="text" class="form-control" id="name" name="name" />
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password"
class="form-control is-invalid" id="password" name="password"/>
<div id="help" class="form-text"></div>
</div>
<div class="text-center">
<button type="submit" id="submit" class="btn btn-primary" disabled>Log in / Create Account</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script>
function validate () {
var username = document.getElementById("email").value;
var email = document.getElementById("username").value;
var name = document.getElementById("name").value;
var password = document.getElementById("password").value;
if (username.length > 0 && email.length > 0 && name.length > 0 && password.length > 0) {
setAutofill();
return true;
}
alert("Username, Email, and Name are all required.");
return false;
}
passwordLength = (num) => {
var password = document.getElementById("password");
var help = document.getElementById("help");
var submit = document.getElementById("submit");
if (password.value.length < num) {
help.textContent = "Password must be at least " + num + " characters";
submit.disabled = true;
} else {
help.textContent = "";
password.classList.remove("is-invalid");
submit.disabled = false;
}
}
setAutofill = () => {
var userInfo = {
"username": document.getElementById("username").value,
"email": document.getElementById("email").value,
"name": document.getElementById("name").value,
}
if(confirm("Do you want to save your info to autofill in the future?")){
localStorage.setItem("userInfo", JSON.stringify(userInfo));
}
}
document.getElementById("password").addEventListener("keyup", function() {
passwordLength(8);
});
autofillOption = () => {
if(localStorage.getItem("userInfo") != null){
console.log(localStorage.getItem("userInfo"));
var autofillButton = document.getElementById("autofill");
autofillButton.disabled = false;
autofillButton.classList.remove("btn-secondary");
autofillButton.classList.add("btn-success");
autofillButton.textContent = "Use Autofill!";
}
}
formFill = () =>{
var userInfo = JSON.parse(localStorage.getItem("userInfo"));
document.getElementById("username").value = userInfo["username"];
document.getElementById("email").value = userInfo["email"];
document.getElementById("name").value = userInfo["name"];
autofill = true;
}
</script>
</body>
</html>