带有一个头文件的不同页面标题?

时间:2012-07-21 10:35:25

标签: php

如何使用头文件执行多个页面标题?虽然有一件事。对于索引页面,我有

error_reporting(0);
if ($_GET["error"]=="404") { 
    include("forum/styles/art_air/web_template/overall_header.php");
    include("include/404"); 
    include("include/index");
    include("forum/styles/art_air/web_template/overall_footer.php");
} else { 
    include("forum/styles/art_air/web_template/overall_header.php");
    include("include/index");
    include("forum/styles/art_air/web_template/overall_footer.php");
}

所以我会在其他任何事情之前得到标题。那么我将如何做到这一点

index?error=404和索引有不同的标题?提前谢谢。

3 个答案:

答案 0 :(得分:2)

overall_header.php

<?php
$title = "Hello, wolrd!";
if ( $_GET["error"] == "404" ) {
    $title = "Error";
}
?>

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

答案 1 :(得分:0)

使用JavaScript和document.title

示例:

<script language="javascript">document.title = "My Title"</script>

JS可以在身体中使用。


另一种方法是在包含所有内容之前设置$GLOBAL变量。

示例:

error_reporting(0);
$GLOBALS['404'] = 0;
if ($_GET["error"]=="404") { 
    $GLOBALS['404'] = 1;
    include("forum/styles/art_air/web_template/overall_header.php");
    include("include/404"); 
    include("include/index");
    include("forum/styles/art_air/web_template/overall_footer.php");
} else { 
    $GLOBALS['404'] = 0;
    include("forum/styles/art_air/web_template/overall_header.php");
    include("include/index");
    include("forum/styles/art_air/web_template/overall_footer.php");
}

overall_header.php

if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
else echo '<title>My Title</title>';

答案 2 :(得分:0)

您可以尝试切换

<?php
$page = $_SERVER['PHP_SELF'];
switch($page) {
    case "index.php":
        echo "<title>My Homepage</title>";
        break;
    case "apples.php":
        echo "<title>The Best Apples!</title>";
        break;
    case "bananas.php":
        echo "<title>We Sell Bananas</title>";
        break;
}
?>