• Php script for determining the operator by ip. Definition of the visitor's city. PHP GeoIP - standard third party functionality

    Display the visitor's city to improve the store's conversion rates. Ready-made embed code anywhere on the site with the city, region and country. I shoveled a lot of scripts, but this one turned out to be the simplest and most effective! I used it when creating the site www.mzm-market.ru, where the script displays "delivery to ______"

    Paste the following code into index.tpl (or on the delivery page) in any desired place:



    window.onload =function()(
    jQuery("#user-city").text(ymaps.geolocation.city);
    jQuery("#user-region").text(ymaps.geolocation.region);
    jQuery("#user-country").text(ymaps.geolocation.country);)


    The output is made using:

    // country, city, region.
    geolocation.country, geolocation.city, geolocation.region

    I want to supplement this post a little (edited on 02/18/2017).

    When using the above script in many cities and browsers, nothing was displayed, and after a long search, I found probably the most efficient script for determining the country, region, city through the Yandex API. I tested it through my clients, who are located almost all over Russia and abroad, and this script proved to be excellent! Here is a screenshot of my client:

    You understand that everything was clearly defined! Well, I will not torment you with writing and give an example!

    js Determination of the country, region, city through the Yandex API

    Just paste this code between tags




    $(document).ready(function()(
    ymaps.ready(function()(
    var geolocation = ymaps.geolocation;
    $("#cou").html("Your country: "+geolocation.country);
    $("#tow").html("Your city: "+geolocation.city);
    $("#reg").html("Your region: "+geolocation.region);
    });
    });

    And this code in the right place on your page:

    Your country: ... to be determined...
    Your city: ... determined by...
    Your region: ... determined by...

    Demonstration

    Your country: ... to be determined...

    Your city: ... determined by...

    Your region: ... determined by...

    The script works without problems, but it is possible that for those who are in small villages, the city is most likely to be determined incorrectly, but the city of your Internet provider will be determined.

    From the author: on the Internet, your first and last name do not mean anything. Here IP is used to define the user. Knowing it, you can set the city and country of residence of a person, the browser used .... Don't believe? You will understand all this when you study the definition of IP in PHP.

    IP and more

    PHP has a global array $_SERVER to get the user's network information. Let's display its contents on the screen:

    Now we know that IP is written to REMOTE_ADDR. Where does it come from? All this data is sent by the client during connection establishment. This variable is set in the server settings on the client side.

    Some people use HTTP header values ​​to determine the city by IP in PHP, but this is fundamentally wrong. Any user can set header values ​​for their host. You should also understand what IP you need. For example, the value HTTP_X_FORWARDED_FOR contains the address of the remote client, its proxy, or may be empty. In short, it's not enough to figure it out for one evening.

    As for the user's browser, you can try to determine it using the get_browser () function. But for its use on the client side, the path to browscap.ini must be specified in the php.ini file. Otherwise, you'll get a similar warning:

    We learn the user's country and not only

    Now, using PHP, we will determine the country by IP. But first, let's review all the solutions in this area. Most of these scripts use the $_SERVER REMOTE_ADDR global array element we mentioned. Now the most important thing begins.

    You can use already, in which the received IP is simply sorted depending on its value. I hope it's not a secret for you that this value is assigned to the client (host) depending on its geolocation: country, city and region of the user's residence. The entire functionality is based on the analysis of these ranges.

    Various third-party services like ip-whois.net are also used. With the help of the script, you pass the value of the IP, and it returns you the information about the location of the host.

    In addition, some of the specialized libraries come with their own database, the tables of which contain all IP ranges sorted (by geography). I won't list them. There are enough of these on the web now.

    You decide. The use of a third-party service will reduce the cost of resources and the load on your site. At the same time, using the library with the base will eliminate connection problems.

    The following script uses the TabGeo library to determine the region by PHP IP. Now its official resource does not work. I downloaded the library from GitHub but only used the country.php file. In it, the country of residence of the user is determined by the received IP. And then (according to the ISO code) you can implement redirects to various pages of the site: