-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
240 lines (118 loc) · 7.05 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
session_start();
$error = "";
if (array_key_exists("logout", $_GET)) {
unset($_SESSION);
setcookie("id", "", time() - 60*60);
$_COOKIE["id"] = "";
session_destroy();
} else if ((array_key_exists("id", $_SESSION) AND $_SESSION['id']) OR (array_key_exists("id", $_COOKIE) AND $_COOKIE['id'])) {
header("Location: loggedinpage.php");
}
if (array_key_exists("submit", $_POST)) {
include("connection.php");
if (($_POST["password"] == "") and ($_POST["email"] == "")) {
$error = '<div class="alert alert-danger" role="alert">Email address and password is required</div>';
}
else if (!$_POST['email']) {
$error .= '<div class="alert alert-danger" role="alert">An email address is required</div>';
}
else if (!$_POST['password']) {
$error .= '<div class="alert alert-danger" role="alert">A password is required</div>';
}
else {
if ($_POST['signUp'] == '1') {
$query = "SELECT id FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."' LIMIT 1";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
$error = '<div class="alert alert-danger" role="alert">That email address is already taken.</div>';
} else {
$query = "INSERT INTO `users` (`email`, `password`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".mysqli_real_escape_string($link, $_POST['password'])."')";
if (!mysqli_query($link, $query)) {
$error = '<div class="alert alert-danger" role="alert">Could not sign you up - please try again later.</div>';
} else {
$query = "UPDATE `users` SET password = '".md5(md5(mysqli_insert_id($link)).$_POST['password'])."' WHERE id = ".mysqli_insert_id($link)." LIMIT 1";
$id = mysqli_insert_id($link);
mysqli_query($link, $query);
$_SESSION['id'] = $id;
if (isset($_POST['stayLoggedIn'])) {
setcookie("id", $id, time() + 60*60*24*365);
}
header('Location: loggedinpage.php');
}
}
} else {
$query = "SELECT * FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."'";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
if (isset($row)) {
$hashedPassword = md5(md5($row['id']).$_POST['password']);
if ($hashedPassword == $row['password']) {
$_SESSION['id'] = $row['id'];
if (isset($_POST['stayLoggedIn']) ) {
setcookie("id", $row['id'], time() + 60*60*24*365);
}
header("Location: loggedinpage.php");
} else {
$error = '<div class="alert alert-danger" role="alert"><p><strong>That email/password combination could not be found.</strong></div>';
}
} else {
$error = '<div class="alert alert-danger" role="alert"><p><strong>That email/password combination could not be found.</strong></div>';
}
}
}
}
?>
<?php include("header.php"); ?>
<div class="container" id="homePageContainer">
<h1><span class="title2">Secret </span><i class="fas fa-user-secret"></i><span class="title3"> Diary</span></h1>
<div><strong><span class="title1">Store your thoughts permanently and securely.</span></strong></div>
<div id="error"><?php echo $error; ?></div>
<form method="post" id="signUpForm">
<p><b>Interested? Sign Up now.</b></p>
<div class="form-group">
<label for="exampleInputEmail1"></label>
<input type="email" class="form-control" aria-describedby="emailHelp" name="email" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1"></label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" name="stayLoggedIn" value=1>
<label class="form-check-label" for="exampleCheck1">Stay Logged In</label>
</div>
<div class="form-group">
<input type="hidden" name="signUp" value="1">
<input type="submit" name="submit" value="Sign Up!" class="btn btn-success">
</div>
<p class="bottom"><a class="toggleForms" href="#">Log In</a></p>
</form>
<form method="post" id="logInForm">
<p><b>Log in using your email and password.</b></p>
<div class="form-group">
<label for="exampleInputEmail1"></label>
<input type="email" class="form-control" aria-describedby="emailHelp" name="email" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1"></label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" name="stayLoggedIn" value=1>
<label class="form-check-label" for="exampleCheck1">Stay Logged In</label>
</div>
<div class="form-group">
<input type="hidden" name="signUp" value="0">
<input type="submit" name="submit" value="Log In" class="btn btn-success">
</div>
<p class="bottom"><a class="toggleForms" href="#">Sign up</a></p>
</form>
<div>
<a href='connect.php'><button type="button" class="btn btn-primary" id="ask">For any query? Ask here.</button></a>
<div id="maker">Handcrafted by:Kakubotics</div>
</div>
</div>
<?php include("footer.php"); ?>