Home >Backend Development >PHP Tutorial >How to get the city based on IP

How to get the city based on IP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:55:401452browse
Put this method at the bottom of includes/lib_base.php
/*
*
* How to get the city based on ip
* @param type $ip ip address
* @return boolean|string
*
*/
function GetIpLookup ($ip = ''){
if(empty($ip)){
  return 'Please enter the IP address';
}
$res = @file_get_contents('http://int.dpool.sina.com.cn /iplookup/iplookup.php?format=js&ip=' . $ip);
if(empty($res)){ return false; }
$jsonMatches = array();
preg_match('#{.+?}# ', $res, $jsonMatches);
if(!isset($jsonMatches[0])){ return false; }
$json = json_decode($jsonMatches[0], true);
if(isset($json[ 'ret']) && $json['ret'] == 1){
                  $json['ip'] =  $ip;
}
return $json;
}
Steps to create targeted advertising
1. Our goal is to operate the advertising table, that is, ad. First, we need to add a "position" field to the ad table.
2. When adding advertisements in the background, there needs to be a field that can be selected. Then first find the page for adding advertisements in the background management and "open in a new tab". Find the php file from which the added page comes. [This php file should be in the background, that is, the admin directory. Next] Find the add method and find the display sentence in the add method to find the template for the added page.
3. Add the template of the page [The template is in the admin/template directory] To add the content in a drop-down list, you need to query all the cities in the city table in the add method in ads.php, that is, the data with parent_id 1 and send it to the template. middle. The template loop is displayed.
4. Find the adding method by inspecting the elements on the page [View the original code and search for "act", that is, the one where the input hidden field name is equal to act, see what the value is, it should be insert] Find the adding method in ads.php and find it inside The insert statement puts our newly added "location" field so that it can be successfully added to the database
5. First of all, because the file lib_base.php is introduced in init.php, the method of obtaining ip, that is, real_ip, exists in lib_base. In php, the code in the quick disk is also directly pasted in the last line of this file. At this time, we can call the method of querying the address based on IP in index.php.
In 6.index.php, we query the city’s data in the advertisement table and send the data to the page, where the advertisement we added is displayed on the page.
----------------The fixed-point delivery function ends here------------



The above introduces the method of obtaining the city based on IP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:LEMP架构及应用部署Next article:mac安装swoole