0
我有一個簡單的(excercise)網站,其結構由一個內容選擇菜單和一個內容面板組成,其中當選擇一個菜單元素時顯示內容。 菜單使用?content_id=...
類型的鏈接傳遞信息。如何優雅地處理php內容包含?
我想處理404不存在的ID或缺少的內容代碼,但要做到這一點,我必須計算頁面加載內容面板的內容,以便我可以輸出正確的標題之前的一切。我最終在我的文件開頭寫了這段代碼,並在必要時打印出$content
變量。無論如何,我對這種方法感到很不安,因爲我必須停止自然流動來捕捉輸出。
<?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();
?>
我想不通什麼是最好的辦法做到這一點,我會很高興,如果你能幫助我!
非常非常非常小心直接使用在任何指的是服務器的文件系統用戶提供的輸入。你可能正在輸入一些路徑/文件擴展名數據,但是它仍然允許用戶包含他們想要的任何PHP文件,無論他們是否願意(比如content_id = ../../../seekrit/passwords ) –