Preface
Everyone should have experienced that if it is a large-scale project, it is very important for the website to obtain the user's local information. For example, the 58 Classified Information Network will judge the user when they visit. The location information can then be jumped to the corresponding sub-station, which gives users a good experience instead of all users from all over the country visiting Beijing Station or Shanghai Station.
Using PHP to obtain the location of visitors is widely used in large sites. Of course, in addition to this application, you can also learn many interesting things by analogy. Today I am working on a small project. , used to display different content in different areas of the site, is actually similar to the example above.
Implementation Analysis
To create such a small function, of course, you must first consider using a third-party IP interface. Currently, the larger IP interfaces include Taobao, Sina, and NetEase. QQ, etc., finally chose the API of Sina IP address. It is very simple to use the above. Use the obtained IP address, use the Sina interface, return a status code, and then obtain the user's location based on the status code, and then proceed to each region. Display judgments of different contents.
The PHP code is as follows:
<?php $ip = "218.192.3.42"; $json = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip); $json = json_decode($json, true); echo "IP地址:".$ip;//xiariboke.com echo "归属地:".$json["country"].$json["province"].$json["city"].$json["district"].$json["isp"]; ?>
The JS code is as follows:
<script type="text/javascript" src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" charset="utf-8"></script> <script type="text/javascript"> alert(remote_ip_info.country+" "+remote_ip_info.city); </script>
What we mainly use is PHP code, the IP address here is fixed. If you want to get the visitor's IP address, just change it to $ip = $_SERVER["REMOTE_ADDR"]; The PHP code that displays different contents is as follows:
<?php $ip = $_SERVER["REMOTE_ADDR"]; $json = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip); $json = json_decode($json, true); if($json["province"]=="河北"){ echo "document.writeln(\"河北");\n"; }//xiariboke.com if($json["province"]=="河南"){ echo "document.writeln(\"河南");\n"; } ?>
This code is not organized and optimized. When outputting, I output JS here. It can be changed to any content, even It is the jump URL. In addition, if you want to use it in a static page, it is also very simple. Just include it in JS. The code is as follows:
<script type="text/javascript" src="ip.php?action=test"></script>
action is the parameter passed, If it is no longer useful, you can delete it.
Summary
The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.
For more articles related to using PHP to obtain the location of website visitors, please pay attention to the PHP Chinese website!