PHP重定向基于IP和引用 [英] PHP redirect based on IP AND referrer

查看:129
本文介绍了PHP重定向基于IP和引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我网络中的用户重定向到我们网站上的特定目标网页,具体取决于他们的IP和空白推荐人。此代码有效,但最终会出现在重定向循环中。如何突破重定向循环以正确重定向用户?谢谢!

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();
            } 
}


推荐答案

添加您知道他们已被重定向到该用户的会话:

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();
    } 

}

这篇关于PHP重定向基于IP和引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆