How to obtain a city by ip address

Source: Internet
Author: User
: How to obtain a city from an ip address: this method is put at the bottom of includeslib_base.php *** method for obtaining the city based on ip address * @ paramtype $ ipip address * @ returnboolean | string ** functionGetIpLookup ($ ip) {if (empty ($ ip) {return enter the ip address;} $ res this method is placed at the bottom of javasdes/lib_base.php
/*
*
* How to obtain a city based on an ip address
* @ Param type $ ip address
* @ Return boolean | string
*
*/
Function GetIpLookup ($ ip = ''){
If (empty ($ ip )){
Return '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;
Unset ($ json ['ret ']);
} Else {
Return false;
}
Return $ json;
}
Procedure
1. our goal is to operate the ad table, that is, ad first needs to add a "location" field to the ad table.
2. when adding an advertisement in the background, you need to have a field to select. First, find the page for adding an advertisement in the background management, click "open in new tab", and find the php file from which the page is added. [this php file should be in in the admin Directory, find the add method and find the "display" method in the add method to find the template for adding the page.
3. add the template on the page [under the admin/template directory of the template] to add the content in the drop-down list to ads. in the add method in php, query all cities in the city table, that is, data with parent_id as 1, and send the data to the template. The template is displayed cyclically.
4. find the method to be added through the page review element [view the original code to search for "act", that is, the input hidden domain name is equal to act, and see what the value should be insert] in ads. search for the add method in php and find the insert statement in it to put the newly added "location" field so that 4 can be successfully added to the database.
5. first, because init. the lib_base.php file is introduced in php to obtain the ip address, that is, the real_ip method exists in lib_base.php. Therefore, the code in the quick disk is directly pasted in the last line of the file. at this time, we are in the index. php can call the ip address query method.
6. in index. php, we query the city data in the advertisement table and send the data to the page to display the advertisement we added.
---------------- The fixed-point serving function ends here ------------

The above describes how to obtain the city based on ip addresses, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.