繁体   English   中英

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

[英]How to correctly include a PHP header on every page

我当前的项目通过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生成页面的方式存在重大问题,请说出来! 谢谢

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

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

<?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);
}
?>

我为我的加载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;

并且您可以使用extract()函数将$data键用作变量。

简单

<?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")
?>

关于什么..?

<?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';

尝试使用页面标题重定向到带有$_POST$_GET index.php页面。

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

<?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")

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM