我在 internetz 上找到这段代码,它检查当前页面的 url;
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
所以现在我可以做这样的事情了;
elseif (curPageURL() == "http://www.example.com/pageexample") {
<meta tags here>
}
太棒了。但我也想将其用于分页页面。这些 URL 如下所示:
http://www.example.com/pageexample?start=30&groep=0
http://www.example.com/pageexample?start=60&groep=0
http://www.example.com/pageexample?start=90&groep=0
[....]
http://www.example.com/pageexample?start=270&groep=0
我可以为每个链接使用 if 语句。但我更愿意使用一个。是否可以添加通配符或其他内容?我猜是这样的(注意 *
)
elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {
编辑:我想对所有这些 URL 执行此操作,因为我想给它们相同的 meta description
, <title>
和 <link rel="canonical"
.我可以通过为每个页面 (10+ atm) 执行 if 语句来手动执行此操作,但我认为有更好的方法。
最佳答案
为什么不直接使用 parse_url()
功能?从手册页:
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
?>
// The above would print
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
对于您的特定情况,您可以随后检查 host
和 path
变量。
关于php - 如果当前页面 URL 等于 X 或 X?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997897/