PHP检查推荐网址是否为主页

标签 php http-referer

我想弄清楚如何检查我的其中一个内页的推荐 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/

相关文章:

php - Google Analytics API 使用自定义数据创建自定义标签

PHP使用 session 从数据库中获取clientID

.htaccess - htaccess 仅当 HTTP 引荐来源网址不等于某些内容时才重定向

php - 单击任何一个链接时要禁用的一组链接

php - 在来自数据库的回显中显示文本区域格式

php - 进行ajax调用时如何在cakephp中的过滤器之前跳过

java - 如何发送 'Referer' 的 URL 请求

mysql - Apache 日志文件中的奇怪引荐来源网址 : empty string and what looks like MySQL code

php - HTTP_REFERER 空白,需要替代

php - 这种方法是否足够好/足够安全以向用户显示错误? - PHP