-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
70 lines (59 loc) · 1.88 KB
/
dashboard.php
File metadata and controls
70 lines (59 loc) · 1.88 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
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: index.php');
exit;
}
$username = $_SESSION['username'];
$is_guest = ($username === 'guest');
$notes_file = "notes.db";
// Delete Existing Notes
if (!$is_guest && isset($_GET['delete'])) {
$lines = file($notes_file, FILE_IGNORE_NEW_LINES);
$index = (int) $_GET['delete'];
if (isset($lines[$index])) {
unset($lines[$index]);
file_put_contents($notes_file, implode("\n", $lines));
}
header("Location: dashboard.php");
exit;
}
// Create New Notes
if (!$is_guest && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['note'])) {
$raw_note = trim($_POST['note']);
if ($raw_note !== '') {
$timestamp = date('Y-m-d H:i');
$formatted_note = "[" . $username . " - " . $timestamp . "] " . $raw_note;
file_put_contents($notes_file, $formatted_note . "\n", FILE_APPEND);
}
header("Location: dashboard.php");
exit;
}
// Read All notes
$notes = file($notes_file, FILE_IGNORE_NEW_LINES);
echo "<h2>Welcome, " . htmlentities($username) . "</h2>";
?>
<a href="logout.php">Logout</a>
<?php if ($username === 'admin'): ?>
| <a href="admin.php" style="color:blue;">Admin Panel</a>
<?php endif; ?>
<br><br>
<?php if (!$is_guest): ?>
<form method="POST">
<textarea name="note" placeholder="Write your note here" required></textarea><br>
<button type="submit">Save Note</button>
</form>
<?php else: ?>
<p style="color:gray;">You are logged in as <strong>guest</strong>. Note creation and deletion are disabled.</p>
<?php endif; ?>
<h3>Shared Notes</h3>
<?php
foreach ($notes as $index => $note) {
echo "<div style='border:1px solid black;margin:5px;padding:5px;'>";
echo $note;
if (!$is_guest) {
echo " <a href='?delete=$index' style='color:red; margin-left:10px;'>[Delete]</a>";
}
echo "</div>";
}
?>