-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangePassPage.php
More file actions
135 lines (114 loc) · 5.02 KB
/
Copy pathchangePassPage.php
File metadata and controls
135 lines (114 loc) · 5.02 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
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
<?php
session_start();
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != 1) {
header("Location: Login/error.php");
exit();
}
require 'db.php';
$message = '';
$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
// Validate passwords
if (strlen($new_password) < 8) {
$error = "New password must be at least 8 characters long";
} elseif ($new_password !== $confirm_password) {
$error = "New passwords do not match";
} else {
$user_id = $_SESSION['id'];
$category = $_SESSION['Category'];
// Get current password hash
if ($category == 1) {
$sql = "SELECT fpassword FROM farmer WHERE fid = ?";
} else {
$sql = "SELECT bpassword FROM buyer WHERE bid = ?";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// Verify current password
if (password_verify($current_password, $user[$category == 1 ? 'fpassword' : 'bpassword'])) {
// Update password
$new_password_hash = password_hash($new_password, PASSWORD_BCRYPT);
if ($category == 1) {
$update_sql = "UPDATE farmer SET fpassword = ? WHERE fid = ?";
} else {
$update_sql = "UPDATE buyer SET bpassword = ? WHERE bid = ?";
}
$update_stmt = $conn->prepare($update_sql);
$update_stmt->bind_param("si", $new_password_hash, $user_id);
if ($update_stmt->execute()) {
$message = "Password changed successfully";
} else {
$error = "Error updating password. Please try again.";
}
} else {
$error = "Current password is incorrect";
}
}
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Change Password</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="assets/css/main.css" />
<link rel="stylesheet" href="assets/css/profile.css" />
<script src="js/jquery.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-layers.min.js"></script>
<script src="js/init.js"></script>
</head>
<body class="subpage">
<?php require 'menu.php'; ?>
<div class="profile-container">
<div class="profile-header">
<h2>Change Password</h2>
</div>
<div class="profile-form">
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form method="post" action="">
<div class="form-group">
<label for="current_password">Current Password</label>
<input type="password" class="form-control" name="current_password" id="current_password" required>
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" class="form-control" name="new_password" id="new_password" required>
<small class="form-text text-muted">Password must be at least 8 characters long</small>
</div>
<div class="form-group">
<label for="confirm_password">Confirm New Password</label>
<input type="password" class="form-control" name="confirm_password" id="confirm_password" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Change Password</button>
<a href="profileView.php" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>