|
To detect country of the visitor |
|
|
|
Script to know the country of the visitor of your web page in php. One of the big needs of every web developer takes root at the moment of knowing the country of the visitor well be for redireccionarlos to modules or pages with peculiarities or proper characteristics of every determination, between other functions that can develop in the environment.
Example: If we have a page of products and catalogs (Web Commerce) and want monetariamente will show to the visitor the price of the above-mentioned product or expressed article in his local currency.
Between other infinities of peculiarities.
In fact the process of the detection of the country across nick is realized from the direction IP returned for superglobal as $_SERVER ['REMOTE_ADDR'], taking as a support or a base a series criteria of possibilities stored logically in database files, for his minor later ratification and deduction of his essential component cuatros all to 255.
In alone Php there exist some functions that allow to obtain information about machines connected to a specific network by means of the Functions of Network, but let's not believe that a function or a pair of functions will do all the work to us; on the contrary there will be only bases for the whole assembly.
Certainly the company Maxmind, desarrolladora par excellence of this type of hardware with support for different technologies “GeoIp“, I manage to solve this big problem with 95 % of effectiveness with a set of functions and a file of databases.
To be able to use this tool we will have to discharge the database:
GeoIP.dat.gz
Library of ties and functions:
Geoip.inc
Fianlemente we will realize a test with the following lines:
<?
require ("geoip.inc"); / *requerimos the library or liberia */
$abir_bd = geoip_open ("GeoIP.dat", GEOIP_STANDARD);/* opening and reading of the file using the constant GEOIP_STANDARD as a form of reading that acquires value in geoip.inc */
/* we print nick of the country */
I begin geoip_country_name_by_addr ($abir_bd, $_SERVER ['REMOTE_ADDR']);
/* we liberate memory closing the resorce returned for $abir_bd of apertura* /
geoip_close ($abir_bd);
?>
Disadvantages and Suggestions:
It would be the most serious error that we were including these lines of ours in all pages, let's remember that whenever this one carge was calling if the same the geoip.inc file and GeoIP.dat, the latter overcomes 700 size Kb and can extend greatly the time of load of the page.
Suggestions:
We can store the value of nick or code of the country in a session ($_SESSION) preferably, or in his defect in COOKIES; so that this way only it is read once since logically the visitor will not change IP to whenever I refreshed or sailed on a page.
Emmanuel García De Caro
http://www.blasten.com/mostrar.php/articulos/descarga/18995/?accion=recomendar |