LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To User Get Location Details Using Pure PHP?

How To User Get Location Details Using Pure PHP?

How-To-User-Get-Location-Details-Using-Pure-PHP
Detecting the location of your users can be really useful if you want to personalize your user’s experience when they browse through your website. Want to show a uniquely tailored promotion? Want to change the language of your site or design based on where your users are coming from?

Lots of websites today show dynamic content based on the user’s location to provide relevant info, ads or local places. In this article, we’re going to show you how to detect user location using Geolocation API. A pure-PHP way without any third-party services to retrieve accurate user location.

Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. If your site is hosted on a non-secure origin (such as HTTP) the requests to get the user’s location will no longer function.

Features:

  1. Light Weight.
  2. Pure PHP JavaScript.
  3. Cross Browser.
  4. No External Files.
  5. Free API.
  6. Fully Customizable.

How To User Get Location Details Using Pure PHP?

There are a few easy and understandable steps to achieve your desired functionality using pure PHP that we are gonna share below. Follow each step perfectly.

Recommended For You:
How To Show Black Opacity On Image Hover Through CSS?

PHP:

public function getLocationInfoByIp(){
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = @$_SERVER['REMOTE_ADDR'];
$result = array('country'=>'', 'city'=>'');
if(filter_var($client, FILTER_VALIDATE_IP)){
$ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)){
$ip = $forward;
}else{
$ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=111.68.111.219"));
if($ip_data && $ip_data->geoplugin_countryName != null){
$result['country'] = $ip_data->geoplugin_countryCode;
$result['city'] = $ip_data->geoplugin_city;
}
return $result;
}
// TO Get THe Required Value
getLocationInfoByIp()['country'];

Output:

array (
'geoplugin_request' => '39.57.193.27',
'geoplugin_status' => 200,
'geoplugin_delay' => '2ms',
'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind,
'geoplugin_city' => 'Karachi',
'geoplugin_region' => 'Sindh',
'geoplugin_regionCode' => 'SD',
'geoplugin_regionName' => 'Sindh',
'geoplugin_areaCode' => '',
'geoplugin_dmaCode' => '',
'geoplugin_countryCode' => 'PK',
'geoplugin_countryName' => 'Pakistan',
'geoplugin_inEU' => 0,
'geoplugin_euVATrate' => false,
'geoplugin_continentCode' => 'AS',
'geoplugin_continentName' => 'Asia',
'geoplugin_latitude' => '24.9207',
'geoplugin_longitude' => '67.0657',
'geoplugin_locationAccuracyRadius' => '100',
'geoplugin_timezone' => 'Asia/Karachi',
'geoplugin_currencyCode' => 'PKR',
'geoplugin_currencySymbol' => '₨',
'geoplugin_currencySymbol_UTF8' => '₨',
'geoplugin_currencyConverter' => '180.4154',
)

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
How To Move Data/Variables From JavaScript To PHP And Vice Versa?

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *