php – How can I display my website in only one city?

Question:

How can I display my website in only one city?

The client only wants to be seen in one city all over the world, how can I do that, I would really appreciate your help 🙂


I did what you told me:

https://i.gyazo.com/5c933886ffd28e8287d803c094574009.png

and it sends me this error:

array ( 'geoplugin_request' => '::1', 'geoplugin_status' => 404, 'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.', 'geoplugin_city' => NULL, 'geoplugin_region' => NULL, 'geoplugin_areaCode' => NULL, 'geoplugin_dmaCode' => NULL, 'geoplugin_countryCode' => NULL, 'geoplugin_countryName' => NULL, 'geoplugin_continentCode' => NULL, 'geoplugin_latitude' => NULL, 'geoplugin_longitude' => NULL, 'geoplugin_regionCode' => NULL, 'geoplugin_regionName' => '', 'geoplugin_currencyCode' => NULL, 'geoplugin_currencySymbol' => NULL, 'geoplugin_currencySymbol_UTF8' => '', 'geoplugin_currencyConverter' => '0', )

Answer:

Updated based on Alfredo Paz's comment

In the case of PHP you could resort to the following:

1) Obtain the IP address of the visitor.

<?php
function get_real_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'];
}
echo $mi_ip = get_real_ip();
?>

2) Call a geolocation service, in this case http://www.geoplugin.net :

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

3) Use the values ​​of the obtained object:

array (
  'geoplugin_request' => '100.100.100.100',
  'geoplugin_status' => 200,
  'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.',
  'geoplugin_city' => 'Ciudad',
  'geoplugin_region' => 'Region\',
  'geoplugin_areaCode' => '0',
  'geoplugin_dmaCode' => '0',
  'geoplugin_countryCode' => 'PA',
  'geoplugin_countryName' => 'Pais',
  'geoplugin_continentCode' => 'Co',
  'geoplugin_latitude' => '0.000',
  'geoplugin_longitude' => '-0.000',
  'geoplugin_regionCode' => '00',
  'geoplugin_regionName' => 'Region,
  'geoplugin_currencyCode' => 'COD',
  'geoplugin_currencySymbol' => NULL,
  'geoplugin_currencySymbol_UTF8' => '',
  'geoplugin_currencyConverter' => '0',
)

4) Assign the city and verify that it is the allowed city

$ciudad_permitida= "Santa Lucia";
$ciudad_cliente = $datos_cliente["geoplugin_city"];
if($ciudad_cliente == $ciudad_permitida){
    header("location index.php");
} else {
    header("location error_ciudad_prohibida.php");
}

5) Your error page

<html>
<h1>Este sitio web esta bloqueado para tu localidad</h1>
</html>

6) Complete would be something like:

<?php
function get_real_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'];
}

$mi_ip = get_real_ip();


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

$ciudad_permitida = "Santa Lucia";

$ciudad_cliente = $datos_cliente["geoplugin_city"];

if($ciudad_cliente == $ciudad_permitida){
    header("location index.php");
} else {
    header("location error_ciudad_prohibida.php");
}
?>

It should be noted that you could obtain the IP from where the user obtains the internet, locating it in a different city.

Note: Change the code so that you take the allowed city instead of going through all the cities that can NOT be valid.

I note that if your city is called "barcelona" example, you could find people who would access from Spain and from Venezuela, you would also have to add the condition of your country.

Updated to verify the country and city of origin:

I add the code also verifying the country of origin avoiding conflicts of the same city, for example: Venezuela and Spain have a city called Barcelona

$pais_permitido = "Venezuela";
$ciudad_permitida= "Barcelona";

$pais_cliente = $datos_cliente["geoplugin_countryName"];
$ciudad_cliente = $datos_cliente["geoplugin_city"];

if($pais_cliente == $pais_permitido and
   $ciudad_cliente == $ciudad_permitida){
    header("location index.php");
} else {
    header("location error_ciudad_prohibida.php");
}

In this case, we first verify that the client is in Venezuela and second that it is in the city of Barcelona.

Updated based on Adriá Vilanova's comment

I quote her comment:

As a suggestion, it would be good to mention that IP geolocation services are not very precise and also sometimes fail very significantly. For example, if you live in a city near Madrid (let's take Alcalá de Henares as an example), the IP geolocation service may say that it is in Madrid. That is why I think it is not a perfect security control method, and I think it should be indicated in the answer for those people who are unaware of this

Scroll to Top