GoogleMapAPI combined with PHP to locate login map

Source: Internet
Author: User
Tags floor function
GoogleMapAPI and PHP are used to achieve logon map locating GoogleMapAPI and PHP. The main idea is to record the IP address when a user logs on to the system, and convert the IP address from the IP address to the physical address through the relevant address index, finally, use GoogleMapAPI to combine Google Map API with PHP to locate logon maps.

Google Map API is used in combination with PHP to achieve logon location. The main idea is to record IP addresses when a user logs on to the system, and convert IP addresses to physical addresses through the relevant address index, finally, use the Google Map API to mark the obtained physical address on Google Map.

?

The php platform is built by the ThinkPHP framework. The specific flowchart is as follows:


?

The CoralWry data file is used here. some files are downloaded from the internet. in the past, Rainbow QQ used this file for parsing, but the file may be updated due to the need to update the IP address, the download package is shown below. This file is stored in the root directory.

?

To continue, introduce two files in TP as the external chain call Library, which can be directly put into the ORG Extension Library under the Lib of the TP core folder.

IpLocation class, which is used to extract IP addresses from the DAT file

?

Class IpLocation {var $ fp; var $ firstip; // offset address of the first ip index var $ lastip; // offset address of the last ip index var $ totalip; // total ip address count // * // Constructor, initialize some variables // $ datfile value to the name of the pure IP database, you can modify it by yourself. // * function ipLocation ($ datfile = "CoralWry. dat ") {$ this-> fp = fopen ($ datfile, 'RB') or die (" CoralWry. dat does not exist. please download the pure IP database, 'coralwry. dat 'put in the current directory "); // enable $ this-> firstip = $ this-> get4b () in binary mode (); // The absolute offset of the first ip index $ this-> lastip = $ this-> get4b (); // The absolute offset of the last ip index Ip address shifting $ this-> totalip = ($ this-> lastip-$ this-> firstip)/7; // The total number of ip addresses in the index area is a fixed length of 7 bytes, divide it by 7, register_shutdown_function (array ($ this, "closefp"); // to be compatible with versions earlier than php5, this class automatically closes the ip library without destructor .} // * // Close the ip address Library // * function closefp () {fclose ($ this-> fp );} /// * // Read 4 bytes and decompress the package into long mode. // * function get4b () {$ str = unpack ("V ", fread ($ this-> fp, 4); return $ str [1];} // * // read the redirected offset address // * function getoffset () {$ str = unpack ("V", fread ($ this-> fp, 3 ). chr (0 ); Return $ str [1];} // * // read the detailed ip address information // * function getstr () {$ str = ""; $ split = fread ($ this-> fp, 1); while (ord ($ split )! = 0) {$ str. = $ split; $ split = fread ($ this-> fp, 1);} return $ str;} // * // Convert the ip address from ip2long to an ipv4 Internet address, compress the data into the big-endian byte order // use it to compare the ip address in the index area. // * function iptoint ($ ip) {return pack ("N ", intval (ip2long ($ ip);} // * // Obtain the client ip address // Note: If you want to record the ip address to the server, check whether ip data is secure before writing to the database. // * function getIP () {if (getenv ('http _ CLIENT_IP ') {$ ip = getenv ('http _ CLIENT_IP ');} elseif (getenv ('http _ X_FORWARDED_FOR ') {// Obtain the real ip address of the client accessed by the proxy server $ Ip = getenv ('http _ X_FORWARDED_FOR ');} elseif (getenv ('http _ x_forwarded') {$ ip = getenv ('http _ x_forwarded ');} elseif (getenv ('http _ FORWARDED_FOR ') {$ ip = getenv ('http _ FORWARDED_FOR');} elseif (getenv ('http _ forwarded ')) {$ ip = getenv ('http _ forwarded');} else {$ ip = $ _ SERVER ['remote _ ADDR '];} return $ ip ;} // * // Obtain address information // * function readaddress () {$ now_offset = ftell ($ this-> fp ); // Obtain the current pointer address $ flag = $ this-> getfl Ag (); switch (ord ($ flag) {case 0: $ address = ""; break; case 1: case 2: fseek ($ this-> fp, $ this-> getoffset (); $ address = $ this-> getstr (); break; default: fseek ($ this-> fp, $ now_offset ); $ address = $ this-> getstr (); break;} return $ address;} // * // Obtain the flag 1 or 2 // to determine whether the address is redirected. // * function getflag () {return fread ($ this-> fp, 1 );} // * // use the binary search method to search for ip addresses in the index area // * function searchip ($ ip) {$ ip = gethostbyname ($ ip ); // Convert the domain name to ip $ ip_offset ["Ip"] = $ ip; $ ip = $ this-> iptoint ($ ip); // Convert the ip address to a growth integer $ firstip = 0; // search for the upper boundary $ lastip = $ this-> totalip; // search for the lower boundary $ ipoffset = $ this-> lastip; // initialization is the offset address of the last IP address while ($ firstip <= $ lastip) {$ I = floor ($ firstip + $ lastip)/2 ); // calculate the approximate intermediate record floor function to calculate the maximum integer of a given floating point number. In other words, fseek ($ this-> fp, $ this-> firstip + $ I * 7); // locate the pointer to the intermediate record $ startip = strrev (fread ($ this-> fp, 4 )); // read the starting IP address in the current index area and convert its little-endian byte to the big-endian byte. If ($ ip <$ startip) {$ lastip = $ I-1;} else {fseek ($ this-> fp, $ this-> getoffset ()); $ endip = strrev (fread ($ this-> fp, 4); if ($ ip >$ endip) {$ firstip = $ I + 1 ;} else {$ ip_offset ["offset"] = $ this-> firstip + $ I * 7; break ;}} return $ ip_offset ;} // * // obtain the ip address details // * function getaddress ($ ip) {$ ip_offset = $ this-> searchip ($ ip ); // Obtain the absolute address of the ip address in the index area $ ipoffset = $ ip_offset ["offset"]; $ address ["ip"] = $ ip_offset ["ip"]; Fseek ($ this-> fp, $ ipoffset); // locate the index area $ address ["startip"] = long2ip ($ this-> get4b ()); // start ip address in the index area $ address_offset = $ this-> getoffset (); // Obtain the offset address fseek ($ this-> fp, $ address_offset); // locate the record area $ address ["endip"] = long2ip ($ this-> get4b ()); // end ip address in the record area $ flag = $ this-> getflag (); // read the flag byte switch ($ flag) {case 1: // region 1 region 2 are redirected $ address_offset = $ this-> getoffset (); // read the redirected address fseek ($ this-> fp, $ ad Dress_offset); // locate the pointer to the redirected address $ flag = $ this-> getflag (); // read the flag byte switch (ord ($ flag) {case 2: // region 1 is redirected again, fseek ($ this-> fp, $ this-> getoffset (); $ address ["area1"] = $ this-> getstr (); fseek ($ this-> fp, $ address_offset + 4); // jump 4 bytes $ address ["area2"] = $ this-> readaddress (); // region 2 may be redirected, and there may be no break; default: // region 1, Region 2 does not redirect fseek ($ this-> fp, $ address_offset ); // locate the pointer to the redirected address $ address ["area1"] = $ this-> getstr (); $ address ["area2 "] = $ This-> readaddress (); break;} break; case 2: // region 1 redirection region 2 no redirection $ address1_offset = $ this-> getoffset (); // read the redirected address fseek ($ this-> fp, $ address1_offset); $ address ["area1"] = $ this-> getstr (); fseek ($ this-> fp, $ address_offset + 8); $ address ["area2"] = $ this-> readaddress (); break; default: // region 1 region 2 does not redirect fseek ($ this-> fp, $ address_offset + 4); $ address ["area1"] = $ this-> getstr (); $ address ["area2"] = $ this-> readaddress (); Break;} // * filter some useless data if (strpos ($ address ["area1"], "CZ88.NET ")! = False) {$ address ["area1"] = "unknown";} if (strpos ($ address ["area2"], "CZ88.NET ")! = False) {$ address ["area2"] = "";} return $ address ;}}

?

?

MapService class, Method class

?

?

/*** Google Map Service * 2011.4.8 */import ("ORG. IPA. ipLocation "); class mapService {public static function getIPaddress ($ ip) {// return format $ format =" text "; // Default text, json, xml, js // return code $ charset = "utf8"; // Default UTF-8, gbk or gb2312 $ ip_l = new IpLocation (); $ address = $ ip_l-> getaddress ($ ip); $ address ["area1"] = iconv ('gb2312 ', 'utf-8 ', $ address ["area1"]); $ address ["area2"] = iconv ('gb2312', 'utf-8', $ address ["area2"]); $ add = $ address ["area1"]. $ address ["area2"]; if ($ add = "local address") {$ add = "Hangzhou" ;}return $ add ;}}
?

Then you can call two file classes in the login Action interface function:

?

?

import("ORG.IPA.MapService");        $ipaddress = get_client_ip();$adrInfo=MapService::getIPaddress($ipaddress);
?

?

Next, you can record the above information to the database. on the map page, you can export data from the database and send it to the view layer for display, call the Google Map API at the front-end layer

?

The background control layer code is simple:

?

?

Public function map () {parent: islogin (); $ model = D ("Topicview"); $ list = $ model-> field ('Id, tid, imgid, avatar, address, create_time, topic_from, content, nickname, rootid, homepage ')-> where ("Topic. status = 1 and Topic. type = 'first' ")-> order (" id desc ")-> find (); // dump ($ list); $ this-> assign ('addrlist ', $ list); parent: showSiteInfo ("Lab frontend Lab-Map Position"); $ this-> display ();}
?

?

The following is the front-end template layer js code:

?

?

If (typeof flowg = "undefined" |! Flowg) {var flowg ={};} flowg. initMap = (function () {var htmlString = ''+ '{$ addrList. nickname }:{ $ addrList. content} '+' in {$ addrList. address} '+ ''; var geocoder; var map; var oldinfo = null; function initialize () {geocoder = new google. maps. geocoder (); var latlng = new google. maps. latLng (34.016, 103.535); var myOptions = {zoom: 8, center: latlng, mapTypeId: google. maps. MapTypeId. ROADMAP}; map = new google. maps. map (document. getElementById ("map_canvas"), myOptions); codeAddress ("{$ addrList. address} ", htmlString);} function codeAddress (address, html) {geocoder. geocode ({'address': address}, function (results, status) {if (status = google. maps. geocoderStatus. OK) {map. setCenter (results [0]. geometry. location); var marker = new google. maps. marker ({map: map, positio N: results [0]. geometry. location}); var contentString = html; var infowindow = new google. maps. infoWindow ({content: contentString}); infowindow. open (map, marker); if (oldinfo! = Null) {oldinfo. close () ;}oldinfo = infowindow ;}else {return false ;}}) ;}return initialize ;}) () ;$ (flowg. initMap );
?

?

Previously, google map's marker function was directly called to display information ,:

?


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.