0

你如何在头文件上做多个页面标题?不过有一件事。对于索引页,我有

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和索引有不同的标题?提前致谢。

4

3 回答 3

2

overall_header.php

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

<title><?php echo $title; ?></title>
于 2012-07-21T10:40:25.737 回答
0

使用 JavaScript 和document.title.

例子:

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

JS可以在body中使用。


另一种方法是在包含所有内容之前设置一个$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>';
于 2012-07-21T10:38:15.260 回答
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;
}
?>
于 2012-07-21T10:43:04.603 回答