【问题标题】:Add to page title tag based on variable from URL根据来自 URL 的变量添加到页面标题标签
【发布时间】:2011-04-27 09:45:16
【问题描述】:

我看过下面的帖子,但它有点超出我的范围......

How can I change the <title> tag dynamically in php based on the URL values

基本上,我有一个页面 index.php(其中没有 php,只是为了将来证明而命名 - 也许现在!)。它包含许多灯箱风格的画廊,可以通过 URL 中的变量从外部链接触发 - 例如。 index.php?open=true2、index.php?open=true3 等

我想要 index.php 标题标签 - 包括现有的静态数据 + 基于 URL 变量附加额外的词 - 例如。如果 URL open=true2 添加“汽车画廊”,如果 URL open=true3 添加“猫画廊”,如果 URL 没有变量,则不向标题添加任何内容。

有人可以帮忙吗?我一直在搜索,但要么错过了帖子的重点,要么没有涵盖(达到我的业余水平)。

非常感谢。保罗。

【问题讨论】:

    标签: php url variables tags title


    【解决方案1】:

    在你的 php 脚本的顶部放这个:

    <?php
    
    # define your titles
    $titles = array('true2' => 'Car Gallery', 'true3' => 'Cat Gallery');
    
    # if the 'open' var is set then get the appropriate title from the $titles array
    # otherwise set to empty string.
    $title = (isset($_GET['open']) ? ' - '.$titles[$_GET['open']] : '');
    
    ?>
    

    然后使用它来包含您的自定义标题:

    &lt;title&gt;Pauls Great Site&lt;?php echo htmlentities($title); ?&gt;&lt;/title&gt;

    【讨论】:

    • 我很快就测试了这个,因为它可以将 URL 变量转换为更适合标题的变量。在 Firefox 中工作。非常感谢。
    • 感谢大家的 cmets / 帮助。我会密切关注这个线程 - 特别是任何持续的潜在攻击问题。
    • 很高兴我们能帮助保罗。 @Treffynnon 感谢您添加 htmlentities()。很好的收获。
    【解决方案2】:
    <title>Your Static Stuff <?php echo $your_dyamic_stuff;?></title>
    

    【讨论】:

      【解决方案3】:
      <?php
          if( array_key_exists('open', $_GET) ){
              $title = $_GET['open'];
          }else{
              $title = '';
          }
      ?>
      <html>
      <head>
      <title><?php echo $title; ?></title>
      </head>
      
      <body>
      The content of the document......
      </body>
      
      </html>
      

      http://www.w3schools.com/TAGS/tag_title.asp

      http://php.net/manual/en/reserved.variables.get.php

      【讨论】:

        【解决方案4】:

        PHP 可以从 URL 查询字符串(www.yoursite.com?page=1&cat=dog 等)中获取信息。您需要获取该信息,确保它不是恶意的,然后您可以将其插入到标题中。下面是一个简单的示例 - 对于您的应用程序,请确保您清理数据并检查它不是恶意的:

        <?php
        $open = "";
        
        // check querystring exists
        if (isset($_GET['open'])) {
        // if it does, assign it to variable
        $open = $_GET['open'];
        }
        ?>
        
        <html><head><title>This is the title: <?php $open ?></title></head>
        

        PHP 有很多函数可以转义可能包含讨厌的数据的数据 - 如果您查找 htmlspecialchars 和 htmlentities,您应该会找到有用的信息。

        【讨论】:

        • 是的,但警告它仍然很重要。
        【解决方案5】:

        其他一些答案很容易被滥用,试试这个:

        <?php
            if(array_key_exists('open', $_GET)){
                $title = $_GET['open'];
            } else {
                $title = '';
            }
            $title = strip_tags($title);
        ?>
        <html>
            <head>
                <title><?php echo htmlentities($title); ?></title>
            </head>
            <body>
                    <p>The content of the document......</p>
            </body>
        </html>
        

        否则正如@Ben 所提到的。首先在您的 PHP 中定义您的标题,以防止人们能够直接将文本注入您的 HTML。

        【讨论】:

        • 感谢您将答案的基本细节 - echo htmlentities(..) 添加到已接受的答案中。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 1970-01-01
        • 2010-11-25
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多