php根据IP获取地理位置

piaoling  2013-10-25 17:56:43

http://www.phpandstuff.com/articles/geoip-country-lookup-with-php


How to get the surfers country using PHP? Most coders need to tackle this problem sooner or later. There are several options out there. There is even a package called GeoIP Location. However, since this does not come with most default installations, it may not be your best choice. A company called Maxmind specializes in professional GeoIP solutions. However, when you dig through their website, you can find that they also provide some free databases and free code. I will show you how to use this free stuff in a portable way to easily and quickly add an Ip to Country lookup feature to your PHP script in 2 easy steps.

Step 1. Download database and PHP file

Step 2. The Code

First, include the library and open the geoip database:

// include the php script
include("geoip.inc");

// open the geoip database
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

Getting the country code:

// to get country code
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country code is: $country_code 
";

Getting the country name:

// to get country name
$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country name is: $country_name 
";

And finally, close the geoip database:

// close the database
geoip_close($gi);

This solution uses a very fast and compact binary file as the database, and it does not add any to load to your MySQL server. You can do thousands of lookups per second on very average hardware with this code.

类别 :  默认(739)  |  浏览(68620)  |  评论(1)
发表评论(评论将通过邮件发给作者):

Email:
userhead
2020-04-27 09:51:24

When it comes to Magento shopping cart, customers browsing your store can have a preview about their own shopping cart. Meanwhile, Magento 2 allows store admins to modify the number of products when they have been labeled as an order.


<a href=http://mageguides.com/>magento tutorials</a>
<a href=http://mageguides.com/set-up-magento-2-shopping-cart/>magento persistent shopping cart</a>
<a href=http://mageguides.com/magento-2-marketplace-install-extension/>magento install extensions</a>