-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
188 lines (136 loc) · 4.73 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
<?php
session_start();
ob_start();
$error = "";
$success = "";
if (array_key_exists("logout", $_GET))
{
session_unset ($_SESSION);
$_SESSION["id"] = "";
setcookie("id", "", time() - 60*60);
$_COOKIE["id"] = "";
}
else if ((array_key_exists("id", $_COOKIE) && $_COOKIE['id']))
{
header ("Location:tomriddlesdiary.php");
}
if ($_POST['submit'])
{
include("connection.php");
if ($_POST['email'] == "")
{
$error .= "Email is missing.<br>";
}
if ($_POST['password'] == "")
{
$error .= "Password is missing.<br>";
}
if ($error != "")
{
$error = "The following field(s) were missing:<br>".$error;
}
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 = "This email address is already registered<br>";
}
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 = "Could not sign you up. Please try again later.<br>";
}
else
{
$query = "UPDATE `users` SET `password` = '".md5(md5(mysqli_insert_id($link)).$_POST['password'])."' WHERE id = '".mysqli_insert_id($link)."' LIMIT 1";
mysqli_query($link, $query);
$success = "You have been registered successfully! Please login.";
}
}
}
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 ($_POST['stay-logged-in'] == '1')
{
setcookie("id", $row['id'], time() + 60*60*24*365);
}
header("Location:tomriddlesdiary.php");
}
else
{
$error = "Incorrect Email/Password entered.<br>";
}
}
else
{
$error = "Incorrect Email/Password entered.<br>";
}
}
}
}
?>
<?php include("header.php") ?>
<body>
<div class="container">
<h4>Do you wish to enter the</h4>
<h1>Chamber of Secrets</h1>
<div class="form-container">
<form method="post" id="signup-form">
<div class="form-group">
<input type="email" name="email" class="form-control" id="email" placeholder="Your Email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" id="password" placeholder="Your Password">
</div>
<div class="form-group">
<input type="hidden" name="signup" value=1>
<input type="submit" name="submit" class="btn form-btn" value="Sign Up">
</div>
<p>Already registered? <a class="toggle-form" id="toggle">Login</a></p>
</form>
<form method="post" id="login-form">
<div class="form-group">
<input type="email" name="email" class="form-control" id="email" placeholder="Your Email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" id="password" placeholder="Your Password">
</div>
<div class="form-group">
<input type="checkbox" id="checkbox" name="stay-logged-in" value=1> Stay logged in
</div>
<div class="form-group">
<input type="hidden" name="signup" value=0>
<input type="submit" name="submit" class="btn form-btn" value="Log In">
</div>
<p>Not registered? <a class="toggle-form" id="toggle">Sign in</a></p>
</form>
</div>
</div>
<div>
<?php
if ($error != "")
{
echo "<div class='alert alert-light alert-dismissible fade show'>".$error."<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button></div>";
}
else if ($success != "")
{
echo "<div class='alert alert-light alert-dismissible fade show'>".$success."</div>";
}
?>
</div>
<?php include("footer.php") ?>