Php 根据URL中的变量添加到页面标题标记
我看到了下面的线索,但它有点超出我的理解 基本上,我有一个页面index.php(里面没有php,只是为了将来的证明而命名——也许现在!)。它包含许多lightbox样式的库,这些库可以通过URL中的变量从外部链接触发,例如index.php?open=true2、index.php?open=true3等 我希望index.php title标记-包括现有静态数据+根据URL变量附加其他单词-例如,如果URL open=true2添加“汽车画廊”,如果URL open=true3添加“猫画廊”,如果URL没有变量,则不向标题附加任何内容 有人能帮忙吗?我一直在搜索,但要么错过了帖子的要点,要么没有被覆盖(达到我的业余水平) 非常感谢。保罗。Php 根据URL中的变量添加到页面标题标记,php,url,variables,tags,title,Php,Url,Variables,Tags,Title,我看到了下面的线索,但它有点超出我的理解 基本上,我有一个页面index.php(里面没有php,只是为了将来的证明而命名——也许现在!)。它包含许多lightbox样式的库,这些库可以通过URL中的变量从外部链接触发,例如index.php?open=true2、index.php?open=true3等 我希望index.php title标记-包括现有静态数据+根据URL变量附加其他单词-例如,如果URL open=true2添加“汽车画廊”,如果URL open=true3添加“猫画廊
你的静态素材
<title>Your Static Stuff <?php echo $your_dyamic_stuff;?></title>
文件的内容。。。。。。
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,您应该会找到有帮助的信息。在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']] : '');
?>
然后使用此选项包括您的自定义标题:
Pauls Great Site
其他一些答案可能被滥用,请尝试以下方法:
<?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。是的,但需要注意的是,这一点仍然很重要。我很快测试了这一点,因为它包括将URL变量转换为更适合标题的功能。在Firefox中工作是一种享受。非常感谢。谢谢大家的评论/帮助。我会密切关注这条线索,特别是任何潜在的攻击问题。很高兴我们能帮助保罗@Treffynnon感谢您添加htmlentities()。很好。感谢您在已接受的答案中添加了答案的基本细节-
echo htmlentities(…)
。
<?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>