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

admin.php

179 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
require "check_login.php";
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);
$maxEntries 3;
$shown 0;

// POST-hantering (lägg till, ta bort, redigera)
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: admin.php?success=1');
            exit;
        }
    }

    
// 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: admin.php');
                exit;
            }
        }
    }

    
// Spara redigerat 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: admin.php');
                exit;
            }
        }
    }
    
    
// Ladda entries efter POST
    
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>Adminsida</title>
    <link rel="icon" href="../../m02/Favicon-a.jpg" type="image/x-icon" />
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="infowall">
    <h2>HEJ & VÄLKOMMEN <?php echo strtoupper($_SESSION['username']); ?>!</h2>
    <ul>
        <li>Välkommen till din sida.</li>
        <li>Här kan du logga ut, byta lösenord och radera konto.</li>
        <li>Du kan också skriva ett meddelande i gästboken.</li>
    </ul>
    <div class="admin-welcome">
        <h3>Du är nu inloggad<span class="sp">, </span>välkommen</h3>
        <p>Din roll: <strong><?php echo $_SESSION['role'] ?? 'user'?></strong></p>
        <div class="admin-actions">
            <a href="logout.php">Logga ut</a>
            <a href="change_password.php">Byt lösenord</a>
            <form action="delete_user.php" method="post" style="display:inline;">
                <input type="hidden" name="username" value="<?php echo $_SESSION['username']; ?>">
                <button type="submit" onclick="return confirm('Vill du verkligen radera ditt konto?');">Radera mitt konto</button>
            </form>
        </div>
    </div>

    <!-- Gästbok (senaste <?php echo $maxEntries?> inlägg) -->
    <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>';
        }
        
?>
        <!-- Formulär: lägg till nytt inlägg i gästboken -->
        <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) {
                if (
$shown >= $maxEntries) break;
                
$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>';

                
// 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 {
                    
// Redigera/ta bort-knapp
                    
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>';
                
$shown++;
            }
            
?>
        </div>
        <?php if ($total 3): ?>
            <div style="text-align:center;">
                <a href="guestbook.php" class="back-link">Visa alla inlägg</a>
            </div>
        <?php endif; ?>
    </div>
</div>

<script>
window.addEventListener('DOMContentLoaded', function() {
  var notice = document.getElementById('sparat');
  if (notice) {
    setTimeout(function() {
      notice.style.display = 'none';
    }, 1500); // 1.5 seconds
  }
});
</script>

</body>
</html>