Php script for determining operator by ip. Sypex Geo - quick identification of a city by IP. Find out the user's country and more
Display the visitor's city to improve store conversion rates. Ready-made insertion code anywhere on the site indicating the city, region and country. I tried a lot of scripts, but this one turned out to be the simplest and most effective! I used it when creating the website 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 would like to add a little to this post (edited on February 18, 2017).
When using the above script, in many cities and browsers simply nothing was displayed, and after a long search I found probably the most efficient script for determining a country, region, city via the Yandex API. I tested it through my clients, who are located almost throughout Russia and beyond, and this script showed itself to be excellent! Here is a screenshot of my client:
You understand that everything has been clearly defined! Well, I won’t torment you with writings and will give you an example!
js Determination of country, region, city via Yandex APIJust paste this code between the 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: ... determined...
Your city: ... determined...
Your region: ... determined...
Your country: ... determined...
Your city: ... determined...
Your region: ... determined...
The script works without problems, but it is possible that for those who are in small villages, the city will most likely be determined incorrectly, and the city of your Internet provider will be determined.
From the author: on the Internet, your first and last name means nothing. Here IP is used to determine the user. Knowing it, you can determine the city and country of residence of a person, the browser used…. Don't believe me? You will understand all this when you study the definition of IP in PHP.
IP and moreTo obtain the user's network information in PHP, there is a global array $_SERVER. Let's display its contents on the screen:
Now we know that the IP is recorded in REMOTE_ADDR. Where does it come from? The client sends all this data during connection establishment. This variable is set in the server settings on the client side.
Some people use HTTP header values to determine a city by IP in PHP, but this is fundamentally incorrect. Any user can specify header values for their host. You should also understand what IP you need. For example, the HTTP_X_FORWARDED_FOR value contains the address of the remote client, its proxy, or may be completely empty. In short, there will be enough to sort this out for more than one evening.
As for the user's browser, you can try to determine it using the get_browser() function. But to use it on the client side, the path to browscap.ini must be specified in the php.ini file. Otherwise you will receive a warning like this:
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 global array element $_SERVER REMOTE_ADDR that we mentioned. Now comes the most important part.
You can use already, in which the received IP is simply sorted depending on its value. I hope it is no secret to you that this value is assigned to the client (host) depending on its geolocation: country, city and region of residence of the user. All functionality is based on the analysis of these ranges.
Various third-party services like ip-whois.net are also used. Using the script, you pass the IP value, and it returns you data about the location of the host.
In addition, some 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 plenty of them on the Internet now.
You decide. Using a third-party service will reduce resource costs and the load on your website. At the same time, using the library with the database will eliminate connection problems.
The following script uses the TabGeo library to determine the region by IP PHP. Now its official resource is not working. I downloaded the library from GitHub, but only used the country.php file. It uses the received IP to determine the user’s country of residence. And then (using the ISO code) you can implement redirects to various pages of the site: