-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignUp.php
259 lines (177 loc) · 7.05 KB
/
SignUp.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
session_start();
$error = "";
$success = "";
if (array_key_exists("logout", $_GET)) {
unset($_SESSION);
session_destroy();
setcookie("id", "", time() - 60 * 60 * 24 * 356);
setcookie("id", "", time() - 60 * 60 * 24 * 356);
$_COOKIE["id"] = "";
header("Location: index.php");
} else if ((array_key_exists("id", $_SESSION) and $_SESSION['id']) or (array_key_exists("id", $_COOKIE) and $_COOKIE['id'])) {
header("Location: loggedinpage.php");
}
include("connection.php");
if (array_key_exists('email', $_POST) or array_key_exists('password', $_POST)) {
if ($_POST['email'] == '') {
$error = "<p>Enter Your Email</p>";
} else if ($_POST['password'] == '') {
$error = "<p>Enter Your Password</p>";
} else {
if ($_POST['sign'] == '1') {
$email = mysqli_real_escape_string($link, $_POST['email']);
$pass = mysqli_real_escape_string($link, $_POST['password']);
if ($_POST['password'] != $_POST['ConfirmPassword']) {
$error = "<p> Password does't match</p>";
} else {
$query = "SELECT `id` FROM `diary` WHERE email = '" . mysqli_real_escape_string($link, $_POST['email']) . "' LIMIT 1";
//echo $query;
$result = mysqli_query($link, $query);
//echo $result;
if ($result and mysqli_num_rows($result) > 0) {
$error = "<p>Email already registered..</p>";
} else {
$query = "INSERT INTO `diary`(`email`, `password`) VALUES ('$email','$pass')";
echo "3";
$result = mysqli_query($link, $query);
$ids = mysqli_insert_id($link);
if ($result) {
echo "2";
$query = "SELECT * FROM `diary` WHERE email = '" . mysqli_real_escape_string($link, $_POST['email']) . "'LIMIT 1";
$hashed = md5(md5($ids) . $_POST['password']);
$query = "UPDATE `diary` SET password = '$hashed' WHERE email = '" . mysqli_real_escape_string($link, $_POST['email']) . "'LIMIT 1";
if (mysqli_query($link, $query)) {
$success = "<p>Sign up successful please log in your account.</p>";
}
echo "3";
$_SESSION['id'] = mysqli_insert_id($link);
} else {
$error = "<p> Could not sign you up- Please try again ";
}
}
}
} else if ($_POST['sign'] == '0') {
$query = "SELECT * FROM `diary` WHERE email = '" . mysqli_real_escape_string($link, $_POST['email']) . "'LIMIT 1";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
if (isset($row)) {
echo $row['id'] . "<br>";
echo md5(md5($row['id']) . $_POST['password']);
$codedPassword = md5(md5($row['id']) . $_POST['password']);
if ($codedPassword == $row['password']) {
$_SESSION['id'] = $row['id'];
if (isset($_POST['stayloggedin']) and $_POST['stayloggedin'] == '1') {
setcookie('id', mysqli_insert_id($link), time() + 60 * 60 * 24 * 365);
}
header("Location: loggedinpage.php");
} else {
$error = "<p> Invalid Password or Email id..</p> ";
}
} else {
$error = "<p> Invalid Password or Email id..</p> ";
}
} else if ($_POST['sign'] == '2') {
$query = "SELECT `id` FROM `diary` WHERE email = '" . mysqli_real_escape_string($link, $_POST['email']) . "'LIMIT 1";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_array($result);
$id = $row['id'];
$query = "UPDATE `diary` SET password = '" . md5(md5($id) . $_POST['password']) . "' WHERE id = '" . $id . "' LIMIT 1";
if (mysqli_query($link, $query)) {
$success = "<p>password change successful please log in your account.</p>";
}
} else {
$error = "<p>Email does't exist..please sign up</p>";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<div class="container col-md-6" id="header">
<div id="heading">
<h1 class="display-4">My Secret Diary</h1>
<small id="emailHelp" class="form-text text-muted"><b>Store your best life moments and secret in your online diary.</b></small>
</div>
<div id="error">
<?php
if ($error != "") {
echo "<div id='error' class='alert alert-danger' role='alert'><strong>" . $error . "</strong></div>";
} else if ($success != "") {
echo "<div id='error' class='alert alert-success' role='alert'><strong>" . $success . "</strong></div>";
}
?>
</div>
<div id="signUp">
<small id="emailHelp" class="form-text text-muted">Intrested ? Please sign up</small>
<form method="post">
<div class="form-group">
<input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" id="exampleInputPassword1" minlength="6" placeholder="Password">
</div>
<div class="form-group">
<input type="password" name="ConfirmPassword" class="form-control" id="exampleInputPassword1" placeholder="Confirm Password">
</div>
<input type="hidden" name="sign" value="1">
<button type="submit" name="submit" class="btn btn-success">Sign Up</button>
</form>
<p><b>Already registered </b></h1><a href="#" id="clickLogIn"> Log in</a></p>
</div>
<div id="logIn">
<small id="emailHelp" class="form-text text-muted">Please Log in</small>
<form method="post">
<div class="form-group">
<input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" id="exampleInputPassword1" minlength="6" placeholder="Password">
</div>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" name="stayloggedin" value="1" class="form-check-input">
Remember me
</label>
</div>
<input type="hidden" name="sign" value="0">
<button type="submit" name="submit" class="btn btn-success">Log In</button>
</form>
<p><a href="#" id="changePassword"> Forget password</a></p>
</div>
<div id="forgetpassword">
<small id="emailHelp" class="form-text text-muted">Forget password</small>
<form method="post">
<div class="form-group">
<input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" id="exampleInputPassword1" minlength="6" placeholder="New Password">
</div>
<input type="hidden" name="sign" value="2">
<button type="submit" name="forgetSubmit" class="btn btn-success">Submit</button>
</form>
</div>
</div>
<script src="bootstrap/js/bootstrap.js"></script>
<script type="text/javascript">
$("#clickLogIn").click(function() {
$("#signUp").toggle();
$("#logIn").toggle();
})
$("#changePassword").click(function() {
$("#logIn").toggle();
$("#forgetpassword").toggle();
})
</script>
</body>
</html>