Home > Backend Development > PHP Tutorial > Get the location of website visitors using PHP

Get the location of website visitors using PHP

高洛峰
Release: 2023-03-05 08:18:01
Original
1478 people have browsed it

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"];
?>
Copy after login

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>
Copy after login

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";
}
?>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template