Shopex: Check the geographic location of the user's IP address in the background

Source: Internet
Author: User
Tags fread unpack

In this article, shopex can view the geographic location information corresponding to the user's registered IP address in the background, obtain the information of the pure IP database through the function, and return it to the template without writing it into the database. The function for retrieving pure IP addresses is modified from discuz! Source code. The code for getting IP information can be used as other types of websites with slight modification, supporting UTF-8 and GBK encoding.

Effect before modification:

(1) preparation:

Download the latest pure IP database qqwry. dat

The code for creating the file pure_get.php is as follows:

<? PHP // contains the configuration file include_once ('.. /.. /.. /.. /. /config. PHP '); // ================================================================ /// /function: obtain the IP address // parameter using the user ID: $ mid is the user ID. === function convertmid ($ mid) {$ ip_mid = $ mid; // obtain the IP address $ ip_dbconn = mysql_connect (db_host, db_user, db_password) through the ID of the connected database; if ($ ip_dbconn) {$ ip_dbsel = mysql_select_db, $ ip_dbconn); $ ip_dbquery = 'select reg_ip from ''. db_prefi X. 'members 'Where member_id ='. $ ip_mid; $ ip_dbresult = mysql_query ($ ip_dbquery, $ ip_dbconn); $ ip_addr = mysql_result ($ ip_dbresult, 0, 0); If (! $ Ip_dbresult) {return 'null';} return $ ip_addr;} // close the database mysql_close ($ ip_dbconn );} // ================================================================ /// /function: function for obtaining the real IP address // parameter: $ IP-IP address $ A1-whether it is a Linux system $ A2-whether to return UTF-8 encoding // ================== ======================== function convertip ($ IP, $ A1, $ A2) {// $ is_linux = $ A1; $ is_utf = $ A2; // absolute path of pure database files, using '/' in Linux '/', in Windows, use '\' if ($ is_linux) {$ dat_path = base_dir. '/qqwry. dat ';} else {$ dat_path = B Ase_dir. '\ qqwry. dat';} // check the IP address format if (! Preg_match ("/^ \ D {1, 3 }. \ D {1, 3 }. \ D {1, 3 }. \ D {1, 3} $/", $ IP) {return 'IP address error! ';} // Open the IP address data file. If the prompt 'IP address data file cannot be read, make sure it is the correct pure IP address library! ', There may be a file location issue // so try to put qqwry. dat in the root directory of the website (note that it is not the root directory of the server !) If (! $ FD = @ fopen ($ dat_path, 'rb') {return 'IP address data file cannot be read. Please make sure it is the correct pure IP address library! ';} // Resolve the IP address to calculate the integer number $ IP = explode ('. ', $ IP); $ ipnum = $ IP [0] * 16777216 + $ IP [1] * 65536 + $ IP [2] * 256 + $ IP [3]; // obtain the start and end locations of IP Address data indexes $ databegin = fread ($ FD, 4); $ dataend = fread ($ FD, 4); $ ipbegin = implode ('', unpack ('l', $ databegin); // The unpack () function unpacks data from binary strings. Unpack (format, data) L-Unsigned long (always 32 bit, machine byte order) # $ ipbegin value: 5386001if ($ ipbegin <0) $ ipbegin + = POW (2, 32); $ ipend = implode ('', unpack ('l', $ dataend); If ($ ipend <0) $ ipend + = POW (2, 32 ); $ ipallnum = ($ ipend-$ ipbegin)/7 + 1; $ beginnum = 0; $ endnum = $ ipallnum; // use the Binary Search Method to search for matched IP records from index records $ ip1num = ''; $ ip2num =''; $ ipaddr1 = ''; $ ipaddr2 = ''; while ($ ip1num> $ ipnum | $ ip2num <$ ipnum) {$ Middle = intval ($ endnum + $ beginnum)/2); // The offset pointer reads four bytes of fseek from the index location ($ FD, $ ipbegin + 7 * $ middle); $ ipdata1 = fread ($ FD, 4); If (strlen ($ ipdata1) <4) {fclose ($ FD ); return 'System error';} // convert and grow the extracted data into an integer. If the data is negative, add the power of 2's 32 times $ ip1num = implode ('', unpack ('l', $ ipdata1); If ($ ip1num <0) $ ip1num + = POW (2, 32 ); // if the number of extracted long integers is greater than the value of our IP address, modify the end position for the next cycle if ($ ip1num> $ ipnum) {$ endnum = $ middle; continue ;} // get the next index after obtaining the previous index $ data Seek = fread ($ FD, 3); If (strlen ($ dataseek) <3) {fclose ($ FD); Return 'System error ';} $ dataseek = implode ('', unpack ('l', $ dataseek. CHR (0); fseek ($ FD, $ dataseek); $ ipdata2 = fread ($ FD, 4); If (strlen ($ ipdata2) <4) {fclose ($ FD); Return 'System error';} $ ip2num = implode ('', unpack ('l', $ ipdata2); If ($ ip2num <0) $ ip2num + = POW (2, 32); // if ($ ip2num <$ ipnum) {if ($ middle = $ beginnum) is not found) {fclose ($ FD); R Eturn 'unknown ';} $ beginnum = $ middle;} // the following code is read dizzy. If you do not understand it, read it slowly. $ ipflag = fread ($ FD, 1); if ($ ipflag = CHR (1) {$ ipseek = fread ($ FD, 3); If (strlen ($ ipseek) <3) {fclose ($ FD); Return 'System error';} $ ipseek = implode ('', unpack ('l', $ ipseek. CHR (0); fseek ($ FD, $ ipseek); $ ipflag = fread ($ FD, 1);} if ($ ipflag = CHR (2 )) {$ addrseek = fread ($ FD, 3); If (strlen ($ addrseek) <3) {fclose ($ FD); Return 'System error' ;}$ Ipflag = fread ($ FD, 1); if ($ ipflag = CHR (2) {$ addrseek2 = fread ($ FD, 3 ); if (strlen ($ addrseek2) <3) {fclose ($ FD); Return 'System error';} $ addrseek2 = implode ('', unpack ('l ', $ addrseek2.chr (0); fseek ($ FD, $ addrseek2);} else {fseek ($ FD,-1, seek_cur );} while ($ char = fread ($ FD, 1 ))! = CHR (0) $ ipaddr2. = $ char; $ addrseek = implode ('', unpack ('l', $ addrseek. CHR (0); fseek ($ FD, $ addrseek); While ($ char = fread ($ FD, 1 ))! = CHR (0) $ ipaddr1. = $ char;} else {fseek ($ FD,-1, seek_cur); While ($ char = fread ($ FD, 1 ))! = CHR (0) $ ipaddr1. = $ char; $ ipflag = fread ($ FD, 1); if ($ ipflag = CHR (2) {$ addrseek2 = fread ($ FD, 3 ); if (strlen ($ addrseek2) <3) {fclose ($ FD); Return 'System error';} $ addrseek2 = implode ('', unpack ('l ', $ addrseek2.chr (0); fseek ($ FD, $ addrseek2);} else {fseek ($ FD,-1, seek_cur );} while ($ char = fread ($ FD, 1 ))! = CHR (0) {$ ipaddr2. = $ char ;}} fclose ($ FD); // The final replacement operation returns the result if (preg_match ('/HTTP/I', $ ipaddr2 )) {$ ipaddr2 = '';} $ ipaddr =" $ ipaddr1 $ ipaddr2 "; $ ipaddr = preg_replace ('/cz88.net/is','', $ ipaddr ); $ ipaddr = preg_replace ('/^ s */is', '', $ ipaddr); $ ipaddr = preg_replace ('/S * $/is ','', $ ipaddr); If (preg_match ('/HTTP/I', $ ipaddr) | $ ipaddr = '') {$ ipaddr = 'unknown ';} // If UTF Encoding is required, some conversions are made; otherwise, GBK encoding if ($ is_ut F) {return iconv ("GBK", "UTF-8", $ ipaddr) ;}else {return $ ipaddr ;}}?>

(2) upload files:

Place qqwry. dat in the shopex root directory, and put pure_get.php in the "shopex \ core \ admin \ controller \ member" directory.

(3) modify the code:

Open shopex \ core \ admin \ controller \ member \ CTL. member. php and find the function show_detail ($ nmid)

Add in the function:

// Modify: Obtain the registered IP address information. // note that the convertip parameter must be correct. Otherwise, the parameter de_once ('ure _ get. PHP '); $ ip_addr = convertmid ($ nmid); $ ip_location = convertip ($ ip_addr, 0, 1 ); $ this-> pagedata ['IP _ location'] = $ ip_location;

Open the shopex \ core \ admin \ view \ member \ member_items.html template and find <{$ mem. reg_ip}>

Add:

<label><{$ip_location}></label>

Values: $ ip_location in member_items.html corresponds to pagedata [ip_location '] In show_detail. You can set some other values to test the display result.

(4) test:

View results in the background

(5) Description:

Function convertmid ($ mid) in pure_get.php is used to obtain the registered IP address of shopex with the user ID. This function is not safe, but it does not understand shopex code at present, the method cannot be found. Instead of using the database account and password, calling the config file is a security measure.

If you can directly find the IP address, you can use convertip ($ IP, $ A1, $ A2) to access the pure database and obtain the address information. Remember to add parameters. Otherwise, the IP database cannot be accessed or garbled characters are returned.

==========================================

From: http://blog.csdn.net/zh405123507

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.