dongzaijiao4863
dongzaijiao4863
2012-07-21 10:35

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

已采纳

How do you do multiple page titles with on header file? Theres one thing though. For the index page, i've got

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

So i would have the header before anything else. So how would i manage to make so that

index?error=404 and index have different titles? Thanks in advance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doubailian4459 doubailian4459 9年前

    Use JavaScript and document.title.

    Example:

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

    JS can be used in body.


    Another method is to set a $GLOBAL variable before including everything.

    Example:

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

    In your overall_header.php:

    if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
    else echo '<title>My Title</title>';
    
    点赞 评论 复制链接分享
  • dpw50696 dpw50696 9年前

    You could try a switch

    <?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;
    }
    ?>
    
    点赞 评论 复制链接分享
  • doubingguan3425 doubingguan3425 9年前

    In overall_header.php

    <?php
    $title = "Hello, wolrd!";
    if ( $_GET["error"] == "404" ) {
        $title = "Error";
    }
    ?>
    
    <title><?php echo $title; ?></title>
    
    点赞 评论 复制链接分享