-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
152 lines (123 loc) · 3.83 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
<?php
session_start();
if(array_key_exists("logout", $_GET)) {
unset($_SESSION);
session_destroy();
session_start();
}
else if(array_key_exists("id", $_SESSION) AND $_SESSION["id"]) {
header("Location: login.php");
}
if(array_key_exists("submit", $_POST)) {
$err="";
$p="";
include("connection.php");
if(!$_POST['email']) {
$err.="An email address is required";
}
if(!$_POST['password']) {
$err.="<br> A password address is required";
}
if($err!="")
$err = "<p> Your form had some errors :</p> <br>".$err;
else {
$em=mysqli_real_escape_string($link, $_POST['email']);
$pa=mysqli_real_escape_string($link, $_POST['password']);
if($_POST['signup']=='1') {
$query="SELECT id FROM diary WHERE email = '$em'";
$result=mysqli_query($link, $query);
if(mysqli_num_rows($result)>0) {
$err.="THE EMAIL HAS BEEN TAKEN";
}
else {
$query="INSERT INTO diary (email, password) VALUES ('$em', '$pa')";
if(mysqli_query($link,$query)) {
$iid=mysqli_insert_id($link);
$encp=md5(md5($iid).$pa);
$query = "UPDATE diary SET password = '$encp' WHERE id ='$iid'";
mysqli_query($link,$query);
if($_POST['stay_logged_in']=='1') {
$_SESSION['id']=$iid;
header("Location: login.php");
}
else {
$p="Sign Up Successful";
}
}
}
}
else {
$query="SELECT * FROM diary WHERE email = '$em'";
$result=mysqli_query($link, $query);
$row=mysqli_fetch_array($result);
if(array_key_exists("id", $row)) {
$iid=$row['id'];
$hashPass=md5(md5($iid).$_POST['password']);
if($row["password"]==$hashPass) {
$_SESSION["id"]=$iid;
header("Location: login.php");
}
else {
$err= "<p> Your form had some errors :</p> <br>"."your password is incorrect <br>";
}
}
else {
$err.= "Your email/password is incorrect";
}
}
}
}
?>
<?php include("header.php"); ?>
<div class="container">
<h1>Secret Diary</h1>
<div id="error">
<?php if($err!="") {
echo "<div class='alert alert-danger' role='alert'>".$err." </div>";
}
else if($p!="") {
echo "<div class='alert alert-success' role='alert'>".$p." </div>";
}
?>
</div>
<h4> Store all your thoughts here </h4>
<form method="POST" id="signupform">
<p> Interested? Sign Up Now !! </p>
<fieldset class="form-group">
<input class="form-control" type="email" name="email" placeholder="Your Email">
</fieldset>
<fieldset class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</fieldset>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" name="stay_logged_in" value='1' id="1"> Stay Logged In
</label>
</div>
<fieldset class="form-group">
<input type="hidden" name="signup" value="1">
<input class="btn btn-success" type="submit" name="submit" value="Sign Up">
</fieldset>
<a href="#" class="toggle">Login</a>
</form>
<form method="POST" id="loginform">
<p> Login with your email-id and password !! </p>
<fieldset class="form-group">
<input class="form-control" type="email" name="email" placeholder="Your Email">
</fieldset>
<fieldset class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</fieldset>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" name="stay_logged_in" value='1' id="0"> Stay Logged In
</label>
</div>
<fieldset class="form-group">
<input type="hidden" name="signup" value="0">
<input class="btn btn-success" type="submit" name="submit" value="Login">
</fieldset>
<a href="#"class="toggle"> Sign Up </a>
</form>
</div>
<?php include("footer.php"); ?>;