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.9 KB

guestbook.php

147 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
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
136
137
138
139
140
141
142
143
144
145
146
147
<?php
session_start
();
date_default_timezone_set('Europe/Stockholm');
$filename "guestbook.json";
$entries = [];
if (
file_exists($filename)) {
    
$json file_get_contents($filename);
    
$entries json_decode($jsontrue) ?? [];
}
$total count($entries);

if (
$_SERVER["REQUEST_METHOD"] === "POST") {
  
// Lägg till nytt inlägg
  
if (isset($_POST['message']) && isset($_SESSION['username'])) {
    
$msg trim($_POST['message']);
    
$sessionName $_SESSION['username'];
    if (
$msg !== '') {
        
$entry = [
            
"date" => date('Y-m-d H:i'),
            
"username" => $sessionName,
            
"message" => $msg
        
];
        
$entries[] = $entry;
        
file_put_contents($filenamejson_encode($entriesJSON_PRETTY_PRINT));
        
header('Location: guestbook.php?success=1');
        exit;
    }
  }

  
// Ladda  entries 
  
$entries = [];
  if (
file_exists($filename)) {
      
$json file_get_contents($filename);
      
$entries json_decode($jsontrue) ?? [];
  }
  
$total count($entries);

  
// Ta bort inlägg
  
if (isset($_POST['delete_entry']) && isset($_SESSION['username'])) {
    
$displayIndex intval($_POST['delete_entry']);
    
$realIndex $total $displayIndex;
    if (isset(
$entries[$realIndex])) {
        
$entry $entries[$realIndex];
        if (
$entry['username'] === $_SESSION['username'] || (isset($_SESSION['role']) && $_SESSION['role'] === 'admin')) {
            
array_splice($entries$realIndex1);
            
file_put_contents($filenamejson_encode($entriesJSON_PRETTY_PRINT));
        }
    }
    
header('Location: guestbook.php');
    exit;
  }

  
// Redigera inlägg
  
if (isset($_POST['save_edit']) && isset($_POST['edit_entry']) && isset($_POST['edit_message']) && isset($_SESSION['username'])) {
    
$displayIndex intval($_POST['edit_entry']);
    
$realIndex $total $displayIndex;
    
$newMsg trim($_POST['edit_message']);
    if (isset(
$entries[$realIndex])) {
      
$entry $entries[$realIndex];
      if (
$entry['username'] === $_SESSION['username'] || (isset($_SESSION['role']) && $_SESSION['role'] === 'admin')) {
        
$entries[$realIndex]['message'] = $newMsg;
        
file_put_contents($filenamejson_encode($entriesJSON_PRETTY_PRINT));
      }
    }
    
header('Location: guestbook.php');
    exit;
  }
}

$entries = [];
if (
file_exists($filename)) {
    
$json file_get_contents($filename);
    
$entries json_decode($jsontrue) ?? [];
}
$total count($entries);
?>
<!doctype html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Gästbok</title>
    <link rel="stylesheet" href="style.css">
    <link rel="icon" href="../../m02/Favicon-a.jpg" type="image/x-icon">
</head>
<body>
<div class="guestbook-wrap">
  <h2>Gästbok</h2>
  <?php
    
if (isset($_GET['success']) && $_GET['success'] == '1') {
      echo 
'<div class="notice success" id="sparat">Inlägget är sparat!</div>';
    }
  
?>
  <form method="post">
    <textarea name="message" placeholder="Skriv ett meddelande..." required></textarea>
    <button type="submit">Skicka</button>
  </form>
  <div class="guestbook-entries">
    <?php
    
foreach (array_reverse($entries) as $displayIndex => $entry) {
        
$date $entry['date'];
        
$username $entry['username'];
        
$message $entry['message'];
        echo 
'<div class="guestbook-entry">';
        echo 
'<div style="font-weight:600;color:#7c5a5a;">' $username '</div>';
        echo 
'<div style="font-size:0.95em;color:#888;">' $date '</div>';
        echo 
'<div style="display:block;max-width:100%;word-break:break-word;overflow-wrap:break-word;">' $message '</div>';
        
        
// Om redigera detta inlägg, visa edit-formulär
        
if (
            isset(
$_POST['edit_entry']) &&
            
$_POST['edit_entry'] == $displayIndex &&
            isset(
$_SESSION['username']) && ($_SESSION['username'] === $username || (isset($_SESSION['role']) && $_SESSION['role'] === 'admin'))
        ) {
            echo 
'<form method="post" class="edit-form" style="display:block;margin-bottom:6px;">';
            echo 
'<input type="hidden" name="edit_entry" value="' $displayIndex '">';
            echo 
'<textarea name="edit_message" style="width:90%;min-height:50px;">' $message '</textarea>';
            echo 
'<button type="submit" name="save_edit" class="gb-btn gb-save">Spara</button>';
            echo 
'</form>';
        } else {
            if (isset(
$_SESSION['username']) && ($_SESSION['username'] === $username || (isset($_SESSION['role']) && $_SESSION['role'] === 'admin'))) {
                echo 
'<div class="gb-btn-group" style="display:flex;gap:8px;margin-top:6px;">';
                echo 
'<form method="post" style="display:inline;"><input type="hidden" name="delete_entry" value="' $displayIndex '"><button type="submit" class="gb-btn gb-delete" title="Ta bort"> Ta bort</button></form>';
                echo 
'<form method="post" style="display:inline;"><input type="hidden" name="edit_entry" value="' $displayIndex '"><button type="submit" class="gb-btn gb-edit" title="Redigera"> Redigera</button></form>';
                echo 
'</div>';
            }
        }
        echo 
'</div>';
    }
    
?>
  </div>
  <a href="admin.php" class="back-link">Till admin sidan</a>
  <a href="index.php" class="back-link">Till startsidan</a>
</div>
<script>
  
window.addEventListener('DOMContentLoaded', function() {
  var notice = document.getElementById('sparat');
  if (notice) {
    setTimeout(function() {
      notice.style.display = 'none';
    }, 1500); // 1.5 sekunder (fade tiden som notices ska vara bort)
  }
});
</script>
</body>
</html>