2015-09-30 173 views
-1

我正在使用GeoPlugin測試訪問者正在瀏覽哪些國家,並根據結果重定向它們。目前,我在每個頁面上都有代碼,因此每個頁面都發出相同的請求。是否可以對每個IP地址執行一次查找並緩存結果一段時間,以減少對GeoPlugin的請求數量。我的代碼到目前爲止是:從GeoPlugin緩存IP結果PHP結果

<?php 

$meta = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])); 

if (($meta['geoplugin_countryCode']=='CN' || 
    $meta['geoplugin_countryCode']=='IR' 
    )&&(  
    $meta['geoplugin_request']!=='1.2.3.4' 
    ))  
{ 
    header('Location: http://google.com', true); 
    die(); 
} 

?> 

在此先感謝!

+0

是的,這是可能的。將它存儲在某處並首先在那裏進行查找。 –

+0

$ _SESSION可以節省你的一天,做一次查找,每頁檢查是否存在,如果不做查找 – VeNoMiS

回答

0

我最後的代碼如下,使用會話以上的建議:

<?php 

session_start(); 

if(isset($_SESSION['geoplugin'])) 

{ 
    $meta=$_SESSION['geoplugin']; 
    echo "Using a stored session"; 
} 

else 

{ 
    $meta = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])); 

    $_SESSION['geoplugin'] = $meta; 

    echo "Using a new session"; 
} 

if (
(
    $meta['geoplugin_countryCode']=='CN' || 
    $meta['geoplugin_countryCode']=='HK' || 
    $meta['geoplugin_countryCode']=='TW' || 
    $meta['geoplugin_countryCode']=='AP' || 
    $meta['geoplugin_countryCode']=='ID' || 
    $meta['geoplugin_countryCode']=='KP' || 
    $meta['geoplugin_countryCode']=='KR' || 
    $meta['geoplugin_countryCode']=='MN' || 
    $meta['geoplugin_countryCode']=='MY' || 
    $meta['geoplugin_countryCode']=='PG' || 
    $meta['geoplugin_countryCode']=='PH' || 
    $meta['geoplugin_countryCode']=='VN' || 
    $meta['geoplugin_countryCode']=='IN' || 
    $meta['geoplugin_countryCode']=='BD' || 
    $meta['geoplugin_countryCode']=='RK' || 
    $meta['geoplugin_countryCode']=='RU' || 
    $meta['geoplugin_countryCode']=='PL' || 
    $meta['geoplugin_countryCode']=='BG' 
) 

&& 
(  
    $meta['geoplugin_request']!=='1.2.3.4' 
) 
) 

{ 

header('Location: https://mywebsite.co.uk/denied', true); 
} 

$status = session_status(); 

if($status == PHP_SESSION_DISABLED) 
{ 
echo "Session is Disabled"; 
} 
else if($status == PHP_SESSION_NONE) 
{ 
echo "Session Enabled but No Session values Created"; 
} 
else 
{ 
echo "Session Enabled and Session values Created"; 
} 

?>