A typical wet coast rainy Sunday morning and up early waiting for Canada to take gold in Olympic hockey later today. With sometime on my hands, onto the net I go. Now if you use B2Evolution's blog, you most likely have LiveHits plugin installed. Recently i noticed my Google Maps API was putting all the markers to the left of International Date line and stacking them vertically, what the deuce???.....hmmmm...now that's odd. Now i guess i am being a bit of a stat junkie curious where my readers are coming from and this plugin is a nice graphic way of showing me where, in near real time, my visitors are coming from on this great planet. And now it is broken and been that way for a couple weeks now. With some spare idle time on my hands while i wait for the liquid wake up juice, aka coffee, to kick in, i decided to look into the issue.
So whenever you have an issue with software, troubleshooting 101 says it is always best to start out with the creator's site to see if it is a known issue and has been reported or if a possible fix, patch and/or update is available. So off to the LiveHits site but alas, no recent updates to the plugin and only 1 comment about the issue but no solution. At the time of this post, there are 4 comments awaiting moderation, what those are and if they have the fix?, no clue. Well, i guess i'm going to have to fix it if i want to continue using it.
So after some reading on Google Maps API's and how they work, i did some tests to validate functionality. Next step was to see how the code passed the Latitude and Longitude to Google. It uses the following URL: http://api.hostip.info/get_html.php?position=true&ip=000.000.000.000 where 000.000.000.000 is the IP of your visitor to get city, country, longitude and latitude. Taking my own IP and submitting it to the URL; voila! City, Country, longitude and latitude returned, excellent. Now that i know what is returned and in what order, now it's time to pop the hood and see what the LiveHits PHP code is doing.
After a couple quick scans of the code, i came across this in _livehits.plugin.php file:
// Look up the location of the IP
if (!empty($row->hit_remote_addr)) {
$url = "http://api.hostip.info/get_html.php?position=true&ip=$row->hit_remote_addr";
$rawlocation = $this->fetch_remote_page( $url );
if (!empty($rawlocation)) {
$loc_array = explode(':', $rawlocation);
$location = $loc_array[1];
$country = trim(str_replace('City', '', $loc_array[1]));
$city = trim(str_replace('Latitude', '', $loc_array[2]));
$lat = trim(str_replace('Longitude', '', $loc_array[3]));
$lon = trim($loc_array[4]);
}
}
Hmmm...an array with explode and a string replace. Now i know the api.hostip.info returns the lookup in this order:
Country: CANADA (CA)
City: Wet Coast (Unknown city)
Latitude:
Longitude:
IP:
Now i know the google maps only show latitude so something is up with the longitude. So not quite sure what the variables are holding, i went a couple lines down to this line:
//echo "<br />Lat: $lat <br /> Lon: $lon";
and removed the //, saved the file and uploaded it. Refreshed live hits and now i can see latitude and longitude and right next to longitude is the word "IP". HA, found it! There's the issue, no removal of the word IP! I can only assume the hostip.info site updated the API and would explain why it was only mapping the latitude and not the longitude as it was passing the Longitude with "IP" at the end and failing silently. So with a quick change to the following line to remove the word "IP":
$lon = trim($loc_array[4]);
with this:
$lon = trim(str_replace('IP', '', $loc_array[4]));
Saved the file and uploaded it to my server, refreshed my LiveHits page and then loaded my blog in another tab. Jumped back to LiveHits and voila! My marker is in the correct spot. It is working again
*pats himself on the back*
Now with any fix on the net, use at your own risk and I cannot be held liable if you edit the wrong line and break the page. Now with that out of the way, if you would like the updated file with both the fix as well as longitude and latitude now showing on the LiveHits page, you can download here or make the chnages yourself to the page.
I assume if you're reading this, then you are effected by this issue and are now fixed.
Let me know if this works for you and now time to go watch Canada take GOLD in Olympic Hockey!
Recent comments