forked from fanchyna/ODU-CS418-518-F21
/
user.php
218 lines (191 loc) · 4.47 KB
/
user.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
<?php
session_start();
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="zross/user.css">
<style>
*
{
background-color: aquamarine;
}
div
{
width: 47vw;
height: 90vh;
background-color: white;
}
#top
{
height: 20px;
background-color: aquamarine;
padding-bottom: 1vh;
}
button
{
background-color: whitesmoke;
}
input
{
background-color: white;
}
form
{
float: right;
padding-right: 30vw;
}
form#info
{
background-color: white;
}
form#info>button, form#info>input
{
margin-bottom: 1vh;
}
.table
{
display: table;
margin: 0 auto;
height: 4vh;
}
.tab-content
{
display: none;
height: 86vh;
}
.tab-content:target
{
display: block;
}
a:link
{
text-decoration: none;
}
a:visited
{
text-decoration: none;
}
a:hover
{
text-decoration: none;
}
a:active
{
text-decoration: none;
}
</style>
<link rel="icon" type="image/x-icon" href="zross/favicon.ico">
<?php
if(array_key_exists('pswd_submit', $_POST))
{
change_pswd();
}
if(array_key_exists('email_submit', $_POST))
{
change_email();
}
if(array_key_exists('logout', $_POST))
{
session_unset();
session_destroy();
}
function change_pswd()
{
$host = 'mysql';
$user = 'root';
$pass = 'rootpassword';
$db = 'ProjectB';
$connected = true;
// Create connection
echo "Attempting to connect...";
$conn = new mysqli($host, $user, $pass, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_errno . $conn->connect_error);
echo '<script>alert("Connection failed.")</script>';
$connected = false;
}
echo "Connected successfully";
if($connected && isset($_SESSION["email"]) && $_SESSION["login"] == true)
{
echo $_SESSION["email"];
echo $_SESSION["login"];
$pswd = password_hash($_POST["pswd"], PASSWORD_BCRYPT);
$stmt = $conn->prepare("Update Users Set password = ? Where email = ?");
$stmt->bind_param("ss", $pswd, $_SESSION["email"]);
$result = $stmt->execute();
//password change success or failure
if($result)
{
echo '<script>alert("Password changed.")</script>';
}
if(!$result)
{
echo '<script>alert("Password change failed.")</script>';
}
$conn->close();
return;
}
}
function change_email()
{
$host = 'mysql';
$user = 'root';
$pass = 'rootpassword';
$db = 'ProjectB';
$connected = true;
// Create connection
echo "Attempting to connect...";
$conn = new mysqli($host, $user, $pass, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_errno . $conn->connect_error);
echo '<script>alert("Connection failed.")</script>';
$connected = false;
}
echo "Connected successfully";
if($connected && isset($_SESSION["email"]) && $_SESSION["login"] == true)
{
$email = $_POST["email"];
$stmt = $conn->prepare("Update Users Set email = ? Where email = ?");
$stmt->bind_param("ss", $email, $_SESSION["email"]);
$result = $stmt->execute();
//email change success or failure
if($result)
{
echo '<script>alert("Email changed.")</script>';
$_SESSION["email"] = $email;
}
if(!$result)
{
echo '<script>alert("Email change failed.")</script>';
}
$conn->close();
return;
}
}
?>
</head>
<body>
<div id="top">
<button><a href="login.php">Register/Login</a></button>
<button><a href="user.php">User Account</a></button>
<form action="search.php">
<button>Search</button>
<input type="text" id="search" name="search">
</form>
</div>
<div>
<form id="info" action="user.php" method="post">
<input type="submit" name="pswd_submit" class="button" value="Password" />
<input type="text" id="pswd" name="pswd" method="post">
<br/>
<input type="submit" name="email_submit" class="button" value="Email" />
<input type="text" id="email" name="email">
<input type="submit" name="logout" class="button" value="Log Out" />
</form>
</div>
<footer>Powered by Apache2, MySQL, and PHP 8.</footer>
</body>
</html>