Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/projekt/slutprojekt/
board_random.php
board_travel.php
board_vgy.php
create_comment.php
create_post.php
create_tables.php
darkmode.js
fetch_comments.php
fetch_posts.php
fetch_posts_random.php
forgot_password.php
index.php
login.php
nav.css
nav.php
post.php
register.php
reset_password.php
sql_inject.php
verify.php
post.php
121 lines UTF-8 Windows (CRLF)
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
<?php
session_start();
include ('../../incl/dbconnect.php');
if(!isset($_GET['post_id'])){
die("Ingen post angiven.");
}
$post_id = intval($_GET['post_id']);
// hämta inlägget
$stmt = $dbconn->prepare("SELECT p.post_id, p.content, p.image_url, p.created_at, u.username
FROM Posts_slutprojekt p
JOIN Users_slutprojekt u ON p.user_id = u.user_id
WHERE p.post_id = :post_id");
$stmt->execute([':post_id' => $post_id]);
$post = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$post){
die("Posten hittades inte.");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post #<?php echo htmlspecialchars($post['post_id']); ?></title>
<link rel="stylesheet" href="nav.css">
<style>
.comment { border: 1px solid #ccc; padding: 5px; margin-bottom: 5px; }
</style>
</head>
<body>
<?php include ('nav.php'); ?>
<h1>Inlägg från <?php echo htmlspecialchars($post['username']); ?></h1>
<p><?php echo nl2br(htmlspecialchars($post['content'])); ?></p>
<?php if($post['image_url']): ?>
<img src="<?php echo htmlspecialchars($post['image_url']); ?>" alt="Bild" style="max-width:400px;"><br>
<?php endif; ?>
<p><small>Skapad: <?php echo htmlspecialchars($post['created_at']); ?></small></p>
<h2>Kommentarer</h2>
<!-- container för kommentarer som uppdateras via AJAX -->
<div id="commentsContainer">
<!-- här fylls på av AJAX -->
</div>
<h2>Lägg till en kommentar</h2>
<!-- formulär för att lägga kommentar -->
<?php if(isset($_SESSION['user_id'])): ?>
<form id="commentForm" action="" method="post">
<textarea name="comment_content" placeholder="Skriv din kommentar..." required rows="4" cols="50"></textarea><br>
<input type="hidden" name="post_id" value="<?php echo htmlspecialchars($post_id); ?>">
<input type="submit" value="Skicka kommentar">
</form>
<?php else: ?>
<p><a href="login.php">Logga in</a> för att kommentera.</p>
<?php endif; ?>
<script>
// funktion för att hämta kommentarer via AJAX
function fetchComments() {
fetch('fetch_comments.php?post_id=<?php echo $post_id; ?>')
.then(r => r.json())
.then(data => {
const container = document.getElementById('commentsContainer');
container.innerHTML = ''; // töm tidigare kommentarer
data.forEach(comment => {
// lägg på stjärna om användaren har mer eller lika med5 posts
let uname = comment.username;
if (comment.post_count >= 5) {
uname += ' 🌟';
}
// bygg HTML
let html = `
<div class="comment">
<p><strong>${uname}</strong> skrev:</p>
<p>${comment.content}</p>
<p><small>${comment.created_at}</small></p>
</div>
`;
container.innerHTML += html;
});
})
.catch(err => console.error('Fel vid hämtning av kommentarer:', err));
}
// uppdatera kommentarer var 3:e sekund
setInterval(fetchComments, 3000);
// hämta kommentarer direkt vid sidladdning
document.addEventListener("DOMContentLoaded", fetchComments);
// AJAX för att posta en ny kommentar
const commentForm = document.getElementById('commentForm');
if(commentForm) {
commentForm.addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
fetch('create_comment.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(result => {
console.log(result);
fetchComments();
commentForm.reset();
})
.catch(error => console.error("Fel vid postning av kommentar:", error));
});
}
</script>
<script src="darkmode.js"></script>
</body>
</html>