-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangePassword.php
More file actions
85 lines (75 loc) · 2.8 KB
/
changePassword.php
File metadata and controls
85 lines (75 loc) · 2.8 KB
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
<script type="text/javascript">
function validateForm()
{
// Check if password matched
if (document.changePwd.pwd1.value != document.changePwd.pwd2.value) {
alert("Passwords not matched!");
return false; // cancel submission
}
return true; // No error found
}
</script>
<?php
// Detect the current session
session_start();
// To Do 1: Check if user logged in
if (! isset($_SESSION["ShopperID"])) {
// redirect to login page if the session variable shopperid is not set
header ("Location: login.php");
exit;
}
// End of To Do 1
$MainContent = "<div style='width:80%; margin:auto;'>";
$MainContent .= "<form name='changePwd' method='post'
onsubmit='return validateForm()'>";
$MainContent .= "<div class='form-group row'>";
$MainContent .= "<div class='col-sm-9 offset-sm-3'>";
$MainContent .= "<span class='page-title'>Change Password</span>";
$MainContent .= "</div>";
$MainContent .= "</div>";
$MainContent .= "<div class='form-group row'>";
$MainContent .= "<label class='col-sm-3 col-form-label' for='pwd1'>
New Password:</label>";
$MainContent .= "<div class='col-sm-9'>";
$MainContent .= "<input class='form-control' name='pwd1' id='pwd1'
type='password' required />";
$MainContent .= "</div>";
$MainContent .= "</div>";
$MainContent .= "<div class='form-group row'>";
$MainContent .= "<label class='col-sm-3 col-form-label' for='pwd2'>
Retype Password:</label>";
$MainContent .= "<div class='col-sm-9'>";
$MainContent .= "<input class='form-control' name='pwd2' id='pwd2'
type='password' required />";
$MainContent .= "</div>";
$MainContent .= "</div>";
$MainContent .= "<div class='form-group row'>";
$MainContent .= "<div class='col-sm-9 offset-sm-3'>";
$MainContent .= "<button type='submit' class='btn btn-danger'>Update</button>";
$MainContent .= "</div>";
$MainContent .= "</div>";
$MainContent .= "</form>";
// Process after user click the submit button
if (isset($_POST['pwd1'])) {
// To Do 2: Read new password entered by user
$new_pwd = $_POST['pwd1'];
// To Do 3: Hash the default password
$hashed_pwd = password_hash($new_pwd, PASSWORD_DEFAULT);
// To Do 4: Update the new password hash
include_once("mysql_conn.php");
$qry = "UPDATE Shopper SET Password=? WHERE ShopperID=?";
$stmt = $conn->prepare($qry);
$stmt->bind_param("si", $hashed_pwd, $_SESSION['ShopperID']);
$stmt->execute();
$stmt->close();
$conn->close();
// Add confirmation text
$MainContent .= "<div class='form-group row'>";
$MainContent .= "<div class='col-sm-9 offset-sm-3'>";
$MainContent .= "<p>Your new password is changed successfully.</p>";
$MainContent .= "</div>";
$MainContent .= "</div>";
}
$MainContent .= "</div>";
include("MasterTemplate.php");
?>