简体   繁体   English

根据 URL 中的变量添加到页面标题标签

[英]Add to page title tag based on variable from URL

I have seen the following thread but it's a bit beyond me...我看过以下线程,但它有点超出我的范围......

How can I change the <title> tag dynamically in php based on the URL values 如何根据 URL 值在 php 中动态更改 <title> 标记

Basically, I have a page index.php (no php in it just named to future proof - maybe now.).基本上,我有一个页面 index.php (其中没有 php 只是为了将来证明而命名 - 也许现在。)。 It contains numerous lightbox style galleries which can be triggered from an external link by a variable in the URL - eg index?php,open=true2.它包含许多灯箱风格的画廊,可以通过 URL 中的变量从外部链接触发 - 例如 index?php,open=true2。 index?php,open=true3.索引?php,open=true3。 etc.等等

I would like the index.php title tag - to include existing static data + append additional words based on the URL variable - eg if URL open=true2 add "car gallery", if URL open=true3 add "cat gallery", if URL has no variable append nothing to title. I would like the index.php title tag - to include existing static data + append additional words based on the URL variable - eg if URL open=true2 add "car gallery", if URL open=true3 add "cat gallery", if URL没有变量 append 没有标题。

Can anyone assist?有人可以帮忙吗? I have been searching but either missed the point of posts or it hasn't been covered (to my amateaur level).我一直在搜索,但要么错过了帖子的重点,要么没有被覆盖(达到我的业余水平)。

Many thanks.非常感谢。 Paul.保罗。

At the top of your php script put this:在 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']] : '');

?>

And then use this to include your custom title:然后使用它来包含您的自定义标题:

<title>Pauls Great Site<?php echo htmlentities($title); ?></title>

<title>Your Static Stuff <?php echo $your_dyamic_stuff;?></title>
<?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://www.w3schools.com/TAGS/tag_title.asp

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

PHP can fetch information from the URL querystring (www.yoursite.com?page=1&cat=dog etc). PHP 可以从 URL 查询字符串中获取信息(www.yoursite.com?page=1&cat=dog 等)。 You need to fetch that information, make sure it's not malicious, and then you could insert it into the title.您需要获取该信息,确保它不是恶意的,然后您可以将其插入到标题中。 Here's a simple example - for your application, make sure you sanitise the data and check it isn't malicious:这是一个简单的示例 - 对于您的应用程序,请确保您清理数据并检查它不是恶意的:

<?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 has lots of functions for escaping data that might contain nasty stuff - if you look up htmlspecialchars and htmlentities you should find information that will help. PHP 为 escaping 数据提供了很多功能,这些数据可能包含讨厌的东西 - 如果您查找 htmlspecialchars 和 htmlentities,您应该会找到有用的信息。

Some of the other answers are open to abuse try this instead:其他一些答案可能会被滥用,请尝试以下方法:

<?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>

Otherwise as @Ben has mentioned.否则正如@Ben 所提到的。 Define you titles in your PHP first to prevent people from being able to directly inject text into your HTML.首先在 PHP 中定义您的标题,以防止人们直接将文本注入您的 HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM