-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
169 lines (123 loc) · 4.52 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
<?php
session_start();
$error="";
// if (array_key_exists("logout", $_GET)) {
// # code...
// unset($_SESSION);
// setcookie("id", "", time() - 60*60);
// $_COOKIE["id"] = "";
// }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("id", $_SESSION) AND $_SESSION['id']) OR (array_key_exists("id", $_COOKIE)AND $_COOKIE['id'])){
header("Location: loggedinpage.php");
}
if (array_key_exists("submit", $_POST )) {
# code...
include("connection.php");
if (!$_POST['email']) {
# code...
$error .= "An Email Address is Required<br>";
}
if (!$_POST['password']) {
# code...
$error .= "A Password is Required<br>";
}
if ($error !="") {
# code...
$error = "<p>There were error(s) in your form:</p>".$error;
}
else{
if ($_POST['signup'] == 1) {
# code...
$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) {
# code...
$error = "That email Address is taken";
}
else{
$query = "INSERT INTO users(email, password) VALUES('".mysqli_real_escape_string($link, $_POST['email'])."', '".mysqli_real_escape_string($link, $_POST['password'])."')";
$id = mysqli_insert_id($link);
if(!mysqli_query($link, $query)){
$error = "<p> Could not sign you up - please try again later.</p>";
}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);
$_SESSION['id'] = $id;
// echo $_SESSION['id'];
if ($_POST['stayLoggedIn'] == 1) {
# code...
setcookie("id", mysqli_insert_id($link), 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)) {
# code...
$hashedpassword = md5(md5($row['id']).$_POST['password']);
if ($hashedpassword == $row['password']) {
# code...
$_SESSION['id'] = $row['id'];
if ($_POST['stayLoggedIn'] == 1) {
# code...
setcookie("id", $row['id'], time() + 60*60*24*365);
}
header("Location: loggedinpage.php");
}else{
$error = "That email/password combination Could not be found.";
}
}else{
$error = "That email/password combination Could not be found.";
}
}
}
}
?>
<?php include("header.php");?>
<div class="container" id="homepagecontainer">
<h1>Secret Diary</h1>
<p><strong>Store your thoughts permanently and Securely.</strong></p>
<div id="error"><?php echo $error;?></div>
<form method="post" id="signinform">
<p>Interested, sign up now!</p>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Your Email">
</div>
<div class="form-group">
<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>
<input type="hidden" name="signup" value="1">
</div>
<div class="form-group">
<input type="submit" class="btn btn-success"name="submit" value="sign Up!" >
</div>
<p><a href="#" class="toggleform">Log In</a></p>
</form>
<form method="post" id="loginform">
<p>Login Using your username and Password</p>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Your Email">
</div>
<div class="form-group">
<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>
<input type="hidden" name="signup" value="0">
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" name="submit" value="Log In!" >
</div>
<p><a href="#" class="toggleform">Sign Up</a></p>
</form>
</div>
<?php include("footer.php")?>