-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatepassword.php
54 lines (53 loc) · 2.04 KB
/
updatepassword.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
<?php
if($_SERVER['REQUEST_METHOD'] != 'POST') {
$end_url = "profile.php";
ob_start();
header("Location: ".$end_url);
ob_end_flush();
die();
}
#grab $connection mysqli object
require 'database.php';
session_start();
$username = $_SESSION['username'];
if(isset($_POST['current_password']) && !empty($_POST['current_password'])){
if(isset($_POST['new_password']) && !empty($_POST['new_password']) && isset($_POST['c_new_password']) && !empty($_POST['c_new_password'])){
$grab_pw_query = "SELECT password FROM Users WHERE username='$username';";
$pw_result = mysqli_query($connection, $grab_pw_query);
$pw_row = $pw_result->fetch_assoc();
$password = $pw_row['password'];
$current_password_raw = $_POST['current_password'];
$current_password = md5($current_password_raw);
if($password != $current_password) {
$_SESSION['password_message'] = '<font color="red" >Your current password is incorrect!</font color="red" >';
$end_url = "profile.php";
ob_start();
header("Location: ".$end_url);
ob_end_flush();
exit();
}
$new_password = md5($_POST['new_password']);
$c_new_password = md5($_POST['c_new_password']);
if($new_password != $c_new_password){
$_SESSION['password_message'] = '<font color="red" >Your new password and the confirmation don\'t match!</font color="red" >';
$end_url = "profile.php";
ob_start();
header("Location: ".$end_url);
ob_end_flush();
exit();
}
$update_pw_query = "UPDATE Users SET password='$new_password' WHERE username='$username';";
$update_pw_result = mysqli_query($connection, $update_pw_query);
$_SESSION = "Your password has been changed";
exit();
}
else {
$_SESSION['password_message'] = '<font color="red" >You must also input your new password and confirm!</font color="red" >';
$end_url = "profile.php";
ob_start();
header("Location: ".$end_url);
ob_end_flush();
exit();
}
}
?>