2014-03-30 139 views
0
新的一页

我不是专家,但我想监视我的流量,我试图与检测用户的地理位置代码创建页面,并将其重定向到新的页面如何创建重定向页面

但它看起来像什么失踪,我不知道是什么

我尝试使用这个Geo redirect user just once using php

但我找不到在哪里我得到这个GeoIP.php

能有人给我小费我需要什么,使工作

感谢 波阿斯

+0

GeoIP.php是我想[MaxMind GeoIP](http://dev.maxmind.com/geoip/)。 – MamaWalter

回答

0

你应该检查了这一点:http://www.php.net/manual/en/function.geoip-continent-code-by-name.php

只需使用

$country = geoip_continent_code_by_name ($_SERVER['REMOTE_ADDR']); 

$国家将返回2个字母的国家代码。这里是你应该能够使用的:

<?php 

    //Get the country code of the user, using REMOTE_ADDR is the most reliable way to do this 
    $country = geoip_continent_code_by_name ($_SERVER['REMOTE_ADDR']); 

    //Create a switch case to deal with the country codes 
    switch((string)$country) { 
    case 'AU': 
     $url = "http://www.site.au"; 
     break; 
    case 'CA': 
     $url = "http://www.site.ca"; 
     break; 

    //default case means, if you didn't provide a case (country code in your example), do this (otherwise known as a "catch all") 
    default: 
     $url = "http://defaultsite.com"; 

     } //End switchcase 

    //This if statement says as long as $url is not empty (! means not), update header location (this is the php way of forwarding) 
    if (!empty($url)){ 
     header('Location: '.$url); 
    } //End if statement 

?> 

你会注意到我删除了try,catch块。这是用户的偏好,但是,大多数人不喜欢它(使用开关盒,这就是为什么你提供了一个默认情况)。

这应该为你工作100%。

+0

如果你从我的帖子了解我不是专家我需要的代码,这将有助于完成这项工作我是总noob – boazor

+0

我继续前进和更新我的文章。 – user1890328