How to target a specific city to show your website in PHP

Suppose you are providing a service for a specific city. You want to show your website only for that city. So you can easily create this by using http://www.geoplugin.net and PHP

First, we need to get the visitor’s IP address by using the below function.

<?php
function get_user_ip() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
        return $_SERVER['HTTP_CLIENT_IP'];
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    return $_SERVER['REMOTE_ADDR'];
}

Then we need to call a geolocation service, in this case, we are using http://www.geoplugin.net

$geo_data = var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.get_user_ip())));

The http://www.geoplugin.net return serializes data, so we need to unserialize() it first and by using var_export() we get the structured information from the text.

We can get the response like below

array ( 
	'geoplugin_request' => '156.239.105.112', 
	'geoplugin_status' => 200, 
	'geoplugin_delay' => '1ms', 
	'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.', 
	'geoplugin_city' => 'Kolkata', 
	'geoplugin_region' => 'West Bengal', 
	'geoplugin_regionCode' => 'WB', 
	'geoplugin_regionName' => 'West Bengal', 
	'geoplugin_areaCode' => '', 
	'geoplugin_dmaCode' => '', 
	'geoplugin_countryCode' => 'IN', 
	'geoplugin_countryName' => 'India', 
	'geoplugin_inEU' => 0, 
	'geoplugin_euVATrate' => false, 
	'geoplugin_continentCode' => 'AS', 
	'geoplugin_continentName' => 'Asia', 
	'geoplugin_latitude' => '22.5697', 
	'geoplugin_longitude' => '88.3697', 
	'geoplugin_locationAccuracyRadius' => '20', 
	'geoplugin_timezone' => 'Asia/Kolkata', 
	'geoplugin_currencyCode' => 'INR', 
	'geoplugin_currencySymbol' => '₹', 
	'geoplugin_currencySymbol_UTF8' => '₹', 
	'geoplugin_currencyConverter' => '71.7795', 
)

Now based on that city, just redirect the user to a specific page.

if($geo_data["geoplugin_city"] == 'Kolkata'){
	header("location:index.php");
}else{
	header("location:error_landing.php");
}

By the above code, you can target multiple cities also. Here index.php supply actual content to the target city. But if a user accesses this website from a different city then it will be redirected to “error_landing.php” page.

So the complete would be something like:

<?php
function get_user_ip() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
        return $_SERVER['HTTP_CLIENT_IP'];
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    return $_SERVER['REMOTE_ADDR'];
}


$geo_data = var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.get_user_ip())));

if($geo_data["geoplugin_city"] == 'Kolkata'){
	header("location:index.php");
}else{
	header("location:error_landing.php");
}
Spread the love
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x