I use php GET to check for an ID in an URL, then retrieve tekst from a database that matches that ID. This is the snippet of code that does that:
function getLyric() {
$id = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM lyrics WHERE id = ".$id."") or die(mysql_error());
if ($row = mysql_fetch_assoc($query)) { ?>
<h1><?= $row['title']; ?> lyrics</h1>
<h2><?= $row['author']; ?></h2>
<pre><?= $row['lyrics']; ?></pre>
<?php }
else { header("HTTP/1.1 404 Not Found"); header("Status 404 Not Found"); }
}
And this is how the page looks where I call this function:
<?php include 'includes/header.php' ?>
<?php getLyric(); ?>
<?php include 'includes/footer.php' ?>
Unfortunately the 404 part in the function does not work. I get the following warning.Warning: Cannot modify header information - headers already sent by [redacted] functions.php on line 61
. Line 61 is the 404 part; "else { header}~".
I've learnt from Stackoverflow that this is because the <html><head>
part is already outputed on the page, so therefore I can't change this anymore. This is correct?
Previously when I didn't have the else~ part I just got a blank page. How can I properly send a 404 in this case? If I can't what is the closest option?