我需要一些javascript或php代码来检测用户是否直接链接到我的网站页面,这样我就可以将其重定向到主页。
这可能吗?
检查服务器的主机是否在http_referer中(这是用户访问的最后一个url)。
function is_direct_link() {
return (!empty($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) === false);
}
if (is_direct_link()) {
header('location: http://www.google.com');
exit();
}
<?php
$referrer = $_SERVER['HTTP_REFERER'];
// see if they are visiting from another website
if (!empty($referrer) && stripos($referrer, 'mydomain.com') === false)
{
// redirect them to the main site
header('Location: http://mydomain.com/');
// stop php from going further
exit;
}
像那样的东西(我想)就是你想要的。
如果referrer为空,这将重定向到主页:
<?php
if(!empty($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], 'example.com') header("Location: example.com");
?>
请注意,referrer不是防弹的,但它做了基本的工作。