使用referrer运行脚本

时间:2009-12-23 23:45:22

标签: php .htaccess referrer

我想知道是否可以根据引荐来源网站执行脚本。例如,如果用户从Facebook访问我的网站,那么我希望激活脚本,但如果用户通过谷歌搜索访问该网站,则不会运行该脚本。这可能吗?

4 个答案:

答案 0 :(得分:2)

您应该能够测试$_SERVER['HTTP_REFERER']以查看用户是否来自Facebook并且行为方式不同。

答案 1 :(得分:1)

您的意思是服务器端或客户端脚本吗?

从客户端,您可以通过document.referrer访问引荐来源(是的,使用加倍的'r',即使相应的HTTP标头拼写错误)。例如:

if (document.referrer.toLowerCase().indexOf('//www.example.com')) {
    document.getElementById('message').innerHTML= 'Hello, visitor from example.com';
}

答案 2 :(得分:0)

这是可能的。请记住,引用者可能是欺骗性的,因此您不应该根据其价值来做与安全相关的事情。

答案 3 :(得分:0)

我会做这样的事情:

if (array_key_exists('HTTP_REFERER', $_SERVER) === true)
{
    // this will give you something like google.com or facebook.com
    $domain = str_ireplace('www.', '', parse_url($_SERVER['HTTP_REFERER'], 'PHP_URL_HOST'));

    // check if there is any referer script you want to execute
    if (is_file('path/to/scripts/' . $domain . '.php') === true)
    {
        // include the path/to/scripts/google.com.php for instance
        include('path/to/scripts/' . $domain . '.php');
    }
}