Visa Källkod

The following files exist in this folder. Click to view.

NamnTypStorlek
admin.phpPHP Fil7.4 KB
change_password.phpPHP Fil1.6 KB
check_login.phpPHP Fil183 B
delete_user.phpPHP Fil641 B
guestbook.jsonJSON Fil623 B
guestbook.phpPHP Fil5.5 KB
index.phpPHP Fil1.9 KB
login.phpPHP Fil1.6 KB
logout.phpPHP Fil341 B
register.phpPHP Fil872 B
users.jsonJSON Fil1.8 KB

change_password.php

58 lines UTF-8 Unix (LF) - Type: PHP Fil
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
<?php
session_start
();

$filename "users.json";
$users file_exists($filename) ? (json_decode(file_get_contents($filename), true) ?? []) : [];
$username $_SESSION['username'] ?? '';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
  
$current $_POST['current_password'] ?? '';
  
$new $_POST['new_password'] ?? '';
  
$updated false;

  foreach (
$users as &$user) {
    if ((
$user['username'] ?? '') === $username) {
      if (
password_verify($current$user['password']) && $new !== '') {
        
$user['password'] = password_hash($newPASSWORD_DEFAULT);
        
$updated true;
      }
      break;
    }
  }
  unset(
$user);

  if (
$updated) {
    
file_put_contents($filenamejson_encode($usersJSON_PRETTY_PRINT));
    
header('Location: admin.php?pwchange=success');
    exit;
  }
  
header('Location: change_password.php?error=1');
  exit;
}
?>
<!doctype html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Byt lösenord</title>
    <link rel="stylesheet" href="style.css">
    <link rel="icon" href="../../m02/Favicon-a.jpg" type="image/x-icon">
</head>
<body>
<div class="pwchange-wrap">
  <h2>Byt lösenord</h2>
  <?php
    
if (isset($_GET['error'])) {
      echo 
'<div class="notice error">Fel: Ogiltigt nuvarande lösenord.</div>';
    }
  
?>
  <form method="post" autocomplete="off">
    <input type="password" name="current_password" placeholder="Nuvarande lösenord" required>
    <input type="password" name="new_password" placeholder="Nytt lösenord" required>
    <button type="submit">Uppdatera lösenord</button>
  </form>
  <a href="admin.php" class="back-link">Tillbaka</a>
</div>
</body>
</html>