我想弄清楚如何检查我的其中一个内页的推荐 url 是否是主页。如果主页始终是 www.mysite.com/index.php,这会很容易,但是当它只是 www.mysite.com 时会发生什么?
我知道我可以简单地做
$url = $_SERVER['HTTP_REFERER'];
$pos = strrpos($url, "/");
$page = substr($url, $pos+1, (strlen($url)-$pos+1));
if (substr_count($url, 'index')) echo 'from index ';
但我的 $url 变量中没有 index.php。
最佳答案
parse_url()
可以在这里为您提供帮助。
// An array of paths that we consider to be the home page
$homePagePaths = array (
'/index.php',
'/'
);
$parts = parse_url($_SERVER['HTTP_REFERER']);
if (empty($parts['path']) || in_array($parts['path'], $homePagePaths)) echo 'from index';
注意任何重要的事情都不应该依赖于此。 Referer:
header 可能会从请求中丢失,并且很容易被欺骗。所有主流浏览器都应该按照您的期望行事,但黑客和网络爬虫可能不会。
关于PHP检查推荐网址是否为主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8726117/