I am using a guestbook with pagination. So in the url is something like: http://example.com/guestbook/guestbook.php?page=6 When typing a page in the url which does not exist yet, (for instance page=9999) he gives me the echo: "Be the first to write in the Guestbook" How can i put a 404 to all those pages which do not exist yet?
if (mysqli_num_rows($results) != 0) { // if DB is not empty
// display records
while ($row = mysqli_fetch_array($results))
{ ?>
<div class="wrapper">
<div class="velden"> <!-- for styling echos; CSS -->
<div class="header">
<div class="naam"><?php echo htmlspecialchars($row['name'], ENT_QUOTES, UTF-8, true); ?></div> <!-- echo naam-->
<div class="email"><?php echo htmlspecialchars($row['email'], ENT_QUOTES, UTF-8, true); ?></div> <!-- echo email-->
<div class="tijd"><?php echo $row['datetime']; ?></div> <!-- echo datum en tijd-->
</div>
<div class="bericht"><?php echo htmlspecialchars($row['message'], ENT_QUOTES, UTF-8, true); ?></div> <!-- echo bericht-->
</div> <!-- end velden -->
<?php } ?>
<?php echo pagination($statement,$per_page,$page,$url='?'); ?>
</div>
<?php } else { // DB is emtpy
echo "Be the first to write in the Guestbook";
}
Something like this may help:
else { // DB is emtpy
if (isset($_GET['page']]) {
echo 'Page 404';
} else {
echo "Be the first to write in the Guestbook";
}
}
This extension is although not very nice I would say. You could use your code which you have before the part you posted (where you parse data for mysql query) to achieve the same thing and have the clearer code.
In the else
header('HTTP 1.1 404 Not Found');