2015-09-30 119 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"; 
} 

?>