2012-04-07 81 views
2

一直有這個問題!反正我已經下載Maxminds GEOIP PHP模塊,並將其上傳到我的服務器,然後我把這段代碼到我的index.php頁面MaxMind GEO_IP重定向

<?php 
require_once("geoip/geoip.inc"); 
$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD); 
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); 
geoip_close($gi); 

$my_countriesrow = array('AD','AE','AF'......and so on); 
$my_countrieseuro = array('AN','AT','BA','BE','BG','BY'.......and so on); 
/* $my_country = array('GB','UK'); */ 

if (!in_array(strtolower($country), $my_countriesrow)){ 
    header('Location: https://www.website.com/row/index.php'); 
    exit(); 
} 
elseif (!in_array(strtolower($country), $my_countrieseuro)){ 
    header('Location: https://www.website.com/euro/index.php'); 
    exit(); 
} 
else { 
    header('Location: https://www.website.com/index.php'); 
    exit(); 
} 
?> 

我已經上傳網頁到我的服務器,並在谷歌瀏覽器我得到。 ... 此網頁有重定向循環

The web page at https://www.website.com/index.php has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. 

一樣在Firefox!任何人有任何想法,因爲我全力以赴......

在此先感謝!

-Phillip露水

UPDATE

OK決定下井的.htaccess ROUTE!我已經編輯我的htaccess文件是這樣的....

GeoIPEnable On 
GeoIPDBFile /geoip/GeoIP.dat 

# Europe 
RewriteEngine on 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AM$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AO$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AQ$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AR$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AS$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AW$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AX$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AZ$ 
RewriteRule ^(.*)$ https://www.Website.com/europe$1 [L] 

# Rest Of World 
RewriteEngine on 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AD$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AE$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AF$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AG$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AI$ 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AL$ 
RewriteRule ^(.*)$ https://www.Website.com/row$1 [L] 

但由於某種原因,我得到一個500內部錯誤,甚至把它仍然沒有工作對我來說每燎國後!真的不知道該怎麼做,因爲我一直在使用不同的PHP腳本工作了一個多星期,現在這個。希望任何人都可以提前幫助再次致謝,我要問的原因是有很多國家代碼需要手動輸入,所以不想把它們都放進去,以至於找不到它們!非常感謝!

-Phillip

回答

0

好了,如果沒有你的if語句的比賽,你重定向到/index.php。由於您正在編輯的代碼服務於index.php,因此將請求重定向到/index.php ...重新指向index.php。你可能想要:

$my_countrieseuro = array('AN','AT','BA','BE','BG','BY'.......and so on); 

if (in_array(strtolower($country), $my_countrieseuro)){ 
    // European version 
    header('Location: https://www.website.com/euro/index.php'); 
    exit(); 
} else { 
    // International version 
    header('Location: https://www.website.com/row/index.php'); 
    exit(); 
} 
+0

感謝phihag,但已決定嘗試使用.htaccess,但即使那不工作吧!已更新我原來的問題!無論如何,感謝 – 2012-04-07 17:43:34

0

嗯,我不知道我會這樣做。

爲什麼不用htaccess文件「玩」一下?

檢查我的答案在這裏:

how to redirect domain according to country IP address

我認爲這可能是你的正確的事情,太...

+0

已經嘗試過你的方式,但即時得到500內部服務器錯誤吧!在我原來的問題上提出更新! – 2012-04-07 17:38:05

0

之所以陳述的RewriteCond的大疊在.htaccess文件中不起作用的是它們被邏輯與在一起。

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AM$ [OR] 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AO$ [OR] 
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AQ$ 
RewriteRule ...some special stuff for any of these hosts... 

您需要在每個條件結尾的方括號中包含'local OR'以防止隱式AND。