File size: 2,340 Bytes
bad7dd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header("Location: admin_login.php");
    exit();
}

$conn = new mysqli("localhost", "root", "", "stc_db");
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

$msg = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $old = sha1($_POST['old_password']);
    $new = sha1($_POST['new_password']);
    $confirm = sha1($_POST['confirm_password']);

    $username = $_SESSION['admin'];

    $res = $conn->query("SELECT * FROM admin_users WHERE username='$username' AND password='$old'");
    if ($res->num_rows == 1) {
        if ($new === $confirm) {
            $conn->query("UPDATE admin_users SET password='$new' WHERE username='$username'");
            $msg = "Password updated successfully!";
        } else {
            $msg = "New passwords do not match.";
        }
    } else {
        $msg = "Old password is incorrect.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
  <title>Change Password</title>
  <style>
    body { font-family: Arial; padding: 20px; background: #f0f0f0; }
    .container {
      max-width: 400px; margin: auto; background: white;
      padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    h2 { color: #004e56; text-align: center; }
    input[type="password"] {
      width: 100%; padding: 10px; margin: 10px 0;
      border: 1px solid #ccc; border-radius: 6px;
    }
    button {
      width: 100%; padding: 10px;
      background: #004e56; color: white; border: none; border-radius: 6px;
    }
    .message { color: red; text-align: center; }
    .success { color: green; }
    a { display: block; text-align: center; margin-top: 15px; }
  </style>
</head>
<body>

<div class="container">
  <h2>Change Admin Password</h2>
  <?php if ($msg): ?>
    <div class="message <?= strpos($msg, 'success') !== false ? 'success' : '' ?>"><?= $msg ?></div>
  <?php endif; ?>
  <form method="post">
    <input type="password" name="old_password" placeholder="Old Password" required />
    <input type="password" name="new_password" placeholder="New Password" required />
    <input type="password" name="confirm_password" placeholder="Confirm New Password" required />
    <button type="submit">Change Password</button>
  </form>
  <a href="dashboard.php">← Back to Dashboard</a>
</div>

</body>
</html>