代码之家  ›  专栏  ›  技术社区  ›  ellisgeek

PHP中的动态标题

  •  1
  • ellisgeek  · 技术社区  · 15 年前

    我正在找书名( <title>bla..bla..bla..</title> )要在PHP中使用类似这样的多文件布局进行更改: functions.php包含在index.php中,然后从functions.php调用get_header()以包含page header.php标题标记在头文件中。我想能够从index.php设置标题,我该怎么做?

    例如,这就是我所尝试的:
    index.php:

    <? require_once('includes/functions.php'); global $t; $t = '/home/s0urc3'; get_header();?>
    
    <div id="main">
    
        <h2><a href="NEEDED" rel="bookmark" title="Permanent Link to NEEDED">NEEDED</a></h2>
    
    
        <p class="postmeta"><a href="<?php page_url(yes)?>" class="readmore">Permalink</a> | <span class="date">Revision Date</span></p>
    
            <p>CONTENT AND CRAP</p>
    
        <!-- main ends -->  
    </div>
    <?php /*test*/echo($title);/*test*/ get_footer();?>
    

    头.php:

    <?php //include('functions.php')?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <? title('$t')?>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
    <?php get_theme_css()?>
    </head>
    
    <body>
    <!-- wrap starts here -->
    <div id="wrap">
    
        <!--header -->
        <div id="header">               
            <h1 id="logo-text"><a href="<?php echo (HOME)?>"><img src="<?php get_url('images/Logo.png')?>" alt="S0URC3"><!-- S0URC3 --></a></h1></div>  
    
    <p id="intro">
            Just another poorly coded website!
            </p>
            </div>
            <div id="nav">
                <!-- <div id="navi"><div id="menu" class="fixed"> -->
                <ul class="">
                <li class=""><a href="http://s0urc3.ismywebsite.com">Home</a></li>
                <li class=""><a href="http://blog.s0urc3.ismywebsite.com">Blog</a></li>
                <li class=""><a href="http://forums.s0urc3.ismywebsite.com">Forums</a></li>
                <li class=""> <a href="mailto:ellisgeek@gmail.com?subject=Comments">Comments</a></li>
                <!--<li class="">Clans</li>-->
                <li class=""><a href="http://astro.s0urc3.ismywebsite.com">-astro-</a></li>
                <!--<li class=""><a href="#">Inspiration</a></li>
                <li class=""><a href="#">Resources</a></li>         
                <li class=""><a href="#">Tutorials</a></li>         
                <li class=""><a href="#">WordPress</a></li>-->          
            </ul><!-- </div></div> -->
    
            </div>                  
        <!--header ends-->                  
        </div>
    
        <!-- content-wrap starts -->
        <div id="content-wrap">
    

    函数.php:

    <?php
    require_once('constants.php');
      //===============//
     //Start Functions//
    //===============//
    
    //Gets a file from the domain http://files01.s0urc3.ismywebsite.com/
    function get_url($file)
    {
    echo (FILE_ROOT . $file);
    }
    
    //gets the url of the theme
    function get_theme_css() {echo('<link rel="stylesheet" href="' . FILE_ROOT . 'colourise/style.css" type="text/css" />');}
    
    function get_header() {require_once('includes/header.php');}
    
    function get_footer() {require_once('includes/footer.php');}
    
    //Gets the URL of the current page
    function page_url($p)
    {
        $s = empty($_SERVER["HTTPS"]) ? ''
            : ($_SERVER["HTTPS"] == "on") ? "s"
            : "";
        $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
        $port = ($_SERVER["SERVER_PORT"] == "80") ? ""
            : (":".$_SERVER["SERVER_PORT"]);
            if ($p == 'yes')
      echo ($protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']);
    else
        return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2)
    {
        return substr($s1, 0, strpos($s1, $s2));
    }
    
    //gets the year
    function cur_year() {echo (YEAR);}
    
    function ads($code) {echo('<script type="text/javascript" src="http://links.ismywebsite.com?i='. $code .'"></script>');}
    
    function title($title)
    {echo('<title>Index Of: '.$title.'</title>');}
      //=============//
     //End Functions//
    //=============//
    ?>
    

    p.s.i只有当在index.php中调用functions.php时,所有函数在页眉和页脚中都可用时,才包含函数。

    2 回复  |  直到 13 年前
        1
  •  8
  •   Dereleased    15 年前

    首先,文件 header.php 正在从函数中包含,因此它不能访问全局变量 $t . 您需要执行以下操作之一:

    functions.php :

    function get_header() {
        global $t;
        require_once('includes/header.php');
    }
    

    或者,在 页眉 :

    <?php //include('functions.php')
    global $t;
    ?>
    <!DOCTYPE html>
    <!-- etc -->
    

    这样,您声明的变量将可用于函数的局部变量范围。

    其次,您需要使用双引号或完全不使用引号来调用函数。单引号不能分析变量。

    对吗?

    • title($t);
    • title("$t");
    • title("{$t}");

    错误的

    • title('$t');

    Here is the PHP Manual Page on the String datatype --一定要检查单引号字符串和双引号字符串。

    挑剔的东西:

    • 您应该始终使用完整的开始标签( <?php )而不是短的开放标签( <? )
    • 你的功能,比如 title() ,应该始终返回一个值,并且您可以回送该值,而不是直接从函数内部回送。
    • 这个 strleft() 您实现的函数虽然很聪明,但已经存在。见: strstr() 实际上,这个函数的作用与你想要的相反;我不记得它做了什么。进行。 再次编辑:不,显然我是对的,您将可选的第三个参数传递为true。您通常应该使用已经存在的函数,因为除了某些例外,它们将更快。
        2
  •  0
  •   Rostyslav Dzinko Ankit    13 年前

    尝试改变

    <? title('$t')?>
    

    <? title($t)?>
    

    在你的代码中。

    推荐文章