PHP redirect based on IP AND referrer

半腔热情 提交于 2019-12-04 05:41:26

问题


I'm trying to redirect users within my network to a specific landing page on our website based on their IP and a blank referrer. This code works, but it ends up in a redirect loop. How do I break out of the redirect loop to correctly redirect a user? Thanks!

$visitor = $_SERVER['HTTP_REFERER'];
$clientip = $_SERVER['REMOTE_ADDR'];
$ip = a regex list of IPs;
if (empty($visitor))
{
    if (preg_match($ip, $clientip)) {
        header('Location: http://example.com');
            die();
            } 
}

回答1:


Add a session to that user that you know that they were redirected already:

session_start();
$visitor = $_SERVER['HTTP_REFERER'];
$clientip = $_SERVER['REMOTE_ADDR'];
$ip = a regex list of IPs;
if (empty($visitor))
{

    //add on if they did not redirect yet.
    if (preg_match($ip, $clientip) && 
        (!isset($_SESSION['redirect']) || !$_SESSION['redirect'])) {
        $_SESSION['redirect'] = true;
        header('Location: http://example.com');
        die();
    } 

}


来源:https://stackoverflow.com/questions/6099535/php-redirect-based-on-ip-and-referrer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!