I have a simple (excercise) website whose structure consist of a content selection menu and a content panel, where content is displayed when one the elements of the menu is selected. The menu use links of the type ?content_id=...
to pass the information over.
I would like to handle 404 codes on inexistent ids or missing contents but to do so I have to compute the content of the content panel befor the page loads, so that I can output the correct header before everything else. I ended up writing this piece of code at the beginning of my file and printing the $content
variable when necessary. Anyway, I feel quiet uncomfortable with this approach since I have to stop the natural flow to catch the include
output.
<?php
// if no content is specified in the address, select the home page (0)
$content_id = (isset($_GET['content_id'])) ? $_GET['content_id'] : 0;
//check if selected content exists and put the content inside the $content variable (to be used later)
if ($content_id < 0 OR $content_id > 2 OR !is_file('exContent/content'.$content_id.'.php')) {
header("HTTP/1.0 404 Not Found");
ob_start();
include 'exContent/noContent.php';
}
else {
ob_start();
include 'exContent/content'.$content_id.'.php';
}
$content = ob_get_contents(); ob_end_clean();
?>
I cannot figure out what's the best way to do this, I would be very happy if you can help me!
Could be a lot better but this is a start :)
$includeFile 'exContent/content'.$content_id.'.php';
if ($content_id < 0 OR $content_id > 2 OR !is_file( $includeFile )) {
header("HTTP/1.0 404 Not Found");
$includeFile = 'exContent/noContent.php';
}
ob_start()
include $includeFile;
$content = ob_get_contents();
ob_end_clean();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.