如何在每个页面上正确包含PHP标头

时间:2013-01-31 22:21:14

标签: php templates header include

我当前的项目通过index.php路由所有内容。然后我看到$_SERVER['REQUEST_URI']是什么,检查白名单,然后执行一些逻辑以包含相关的html模板。像这样:

<?php

include("header.html");

$requested_page = $_SERVER['REQUEST_URI'];

if ($requested_page in $whitelist){
    $page_title = $requested_page;
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
}

include ("footer.html")

?>

我的问题是我想在标题模板中使用$page_title,就像这样......

<title><?php echo $page_title?></title>

但显然它不起作用,因为当包含标题时,$ page_title尚未设置。任何人都可以建议如何解决这个问题?可能“输出缓冲”有帮助吗?如果是这样,怎么样? 如果这种使用php生成页面的方式存在重大问题,请说出来!感谢

编辑:我应该补充一点,条件逻辑在我的实际项目中要复杂得多,而且我在这里简化了很多。我不想继续重复我的包括页眉和页脚,所以这就是为什么他们不包括在内。如果可能的话,我真的宁愿让它们脱离所有的逻辑。

5 个答案:

答案 0 :(得分:3)

您可以重新排序代码以实现此目的:

<?php 
$requested_page = $_SERVER['REQUEST_URI'];

if ($requested_page in $whitelist){
    $page_title = $requested_page;
    include("header.html");
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
}else{
    //Make sure we include header if page not in white list
    include("header.html");
}

include ("footer.html")
?>

编辑:实际上考虑一下,你可能可以将逻辑与包括分开:

<?php 
$requested_page = $_SERVER['REQUEST_URI'];
$includes = array();
$includes[] = 'header.html';
if ($requested_page in $whitelist){
    $page_title = $requested_page;
    $includes[] = $requested_page . "_controller.php";
    $includes[] = $requested_page . "_template.html";
}

$includes[] = "footer.html";
foreach($includes as $include){
    include($include);
}
?>

答案 1 :(得分:1)

我为我的加载MVC库编写了这个来加载视图,但我认为这将变得很方便:

$target = "myfile.php";

$data = array(
  'page_title' => 'page title'
);

ob_start();
if(count($data) > 0) extract($data);
include_once($target);
$content = ob_get_clean();

echo $content;

使用$data函数,您可以使用extract()个键作为变量。

答案 2 :(得分:0)

<?php 
$requested_page = $_SERVER['REQUEST_URI'];

$page_title = "";

if ($requested_page in $whitelist) $page_title = $requested_page;

include("header.html");

if(!empty($page_title)) {
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
}

include ("footer.html")
?>

答案 3 :(得分:0)

怎么样??

<?php 
$page_title = $requested_page = $_SERVER['REQUEST_URI'];
if (!in_array($requested_page, $whitelist)) {
    header('Location: http://example.com/your404');
    exit(0);
}

include 'header.html';
include "$requested_page_controller.php";
include "$requested_page_template.html";
include 'footer.html';

答案 4 :(得分:-1)

尝试使用index.php$_POST以及页面标题重定向回$_GET页面。

如果设置了这些,您可以从页面开头阅读它们。

<?php

if (isset($_GET['pagetitle'])){
  echo "<html><head><title>".$_GET['pagetitle']."</title></head>";
}

include("header.html");

$requested_page = $_SERVER['REQUEST_URI'];

if ($requested_page in $whitelist){
    $page_title = $requested_page;
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
    header( 'Location: http://www.yoursite.com/index.php?pagetitle='.
           urlencode('This is the page title'),'"' ) 
}

include ("footer.html")

?>