Cache IP from GeoPlugin PHP result -
i using geoplugin test country visitors browsing from, , redirecting them based on result. @ moment have code on every page, every page making same request. possible lookup once per ip address , cache result while, reduce number of requests geoplugin. code far is:
<?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(); } ?>
thanks in advance!
my final code follows, using session suggested above:
<?php session_start(); if( isset( $_session['geoplugin'] ) ) { $meta=$_session['geoplugin']; echo "using stored session"; } else { $meta = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_server['remote_addr'])); $_session['geoplugin'] = $meta; echo "using 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 disabled"; } else if($status == php_session_none ) { echo "session enabled no session values created"; } else { echo "session enabled , session values created"; } ?>
Comments
Post a Comment