86 lines
3.4 KiB
PHP
86 lines
3.4 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$post_id = $_GET['id'] ?? null;
|
|
|
|
if (!$post_id || !filter_var($post_id, FILTER_VALIDATE_INT)) {
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
$post = null;
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
|
|
$stmt->execute([$post_id]);
|
|
$post = $stmt->fetch();
|
|
} catch (PDOException $e) {
|
|
error_log("DB Error: " . $e->getMessage());
|
|
}
|
|
|
|
// If no post is found, redirect to the homepage
|
|
if (!$post) {
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo htmlspecialchars($post['title']); ?> - My Awesome Blog</title>
|
|
<meta name="description" content="<?php echo htmlspecialchars($post['excerpt']); ?>">
|
|
|
|
<!-- Open Graph -->
|
|
<meta property="og:title" content="<?php echo htmlspecialchars($post['title']); ?>">
|
|
<meta property="og:description" content="<?php echo htmlspecialchars($post['excerpt']); ?>">
|
|
<meta property="og:type" content="article">
|
|
<meta property="og:url" content="http://<?php echo $_SERVER['HTTP_HOST']; ?>/post.php?id=<?php echo $post['id']; ?>">
|
|
<meta property="og:image" content="<?php echo htmlspecialchars($post['image_url']); ?>">
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="/">My Awesome Blog</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-8">
|
|
<article>
|
|
<header class="mb-4">
|
|
<h1 class="fw-bolder mb-1 display-5"><?php echo htmlspecialchars($post['title']); ?></h1>
|
|
<div class="text-muted fst-italic mb-2">Posted on <?php echo date('F j, Y', strtotime($post['created_at'])); ?></div>
|
|
</header>
|
|
<figure class="mb-4">
|
|
<img class="img-fluid rounded" src="<?php echo htmlspecialchars($post['image_url']); ?>" alt="Hero image for the blog post titled '<?php echo htmlspecialchars($post['title']); ?>'">
|
|
</figure>
|
|
<section class="mb-5 post-content fs-5">
|
|
<?php echo nl2br(htmlspecialchars($post['content'])); ?>
|
|
</section>
|
|
</article>
|
|
<a href="/" class="btn btn-outline-primary">← Back to all posts</a>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="footer mt-auto py-3 bg-white border-top">
|
|
<div class="container text-center">
|
|
<span class="text-muted">© <?php echo date("Y"); ?> My Awesome Blog. All Rights Reserved.</span>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|