dowdw44426 2015-06-16 18:29
浏览 46

根据位置重定向页面

Please help me, and be patient since i'm a php virgin.

I'm using Cloudflare and want to redirect every page from outside of Indonesia to a specific page. After a few tries, I can redirect from index.php to each separate destination page.

But the problem comes when I put this redirect code on the destination page. It seems like it loops indefinitely. I want to keep this functionality on, because I don't want a visitor that clicks on a Google search result to end up in the wrong page.

How can I properly check the IP on every page, and if they are from ID, then they will be redirected to index_id.php. And if they are not, they will go to index_ww.php. This without infinite loops, because this code is in both index_id.php and index_ww.php.

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if ($country_code=="ID"){
    $link = 'http://www.myweb.com/index_id.php';
} else {
    $link = 'http://www.myweb.com/index_ww.php';
}
header("location:$link");
exit;
  • 写回答

1条回答 默认 最新

  • douqian3712 2015-06-16 18:45
    关注

    Try adding a query parameter onto the end of the destination URL, like ?redirected=1, and always check for that parameter before performing the redirect code. Here's an example:

    <?php
    
    if (empty($_GET['redirected'])) {
        $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
    
        if ($country_code=="ID"){
            $link = 'http://www.myweb.com/index_id.php?redirected=1';
        } 
        else {
            $link = 'http://www.myweb.com/index_ww.php?redirected=1';
        }
    
        header("location:$link");
        exit;
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向