Solved

GEO lon/lat reliable server and code needed

Posted on 2014-10-19
5
191 Views
Last Modified: 2014-10-22
Currently, I am using freegeoip.net to set the geo lat/lon of a server when it starts up using a bash script.

Bash code for my function;

function get_set_geo()
{
    # Get GEO LAT/LON

    LATLON=$(wget -q -O - http://freegeoip.net/xml | awk -F'<|>' '/Latitude|Longitude/ {print $3}')

    LAT=$(cut -f1 -d" " <<< $LATLON)
    LON=$(cut -f2 -d" " <<< $LATLON)

    echo "Latitude: " $LAT
    echo "Longitude:" $LON

    $CURLSEC -F function=set_geo -F geo_lat=$LAT -F geo_lon=$LON
}

Open in new window


The variable contents are stored to Mysql
geo_lat       decimal(10,8)       No       None       NULL       
geo_lon       decimal(11,8)       No       None       NULL

I was told in another question that this service is not very accurate so I would like to use another one.

I also found http://www.datasciencetoolkit.org/developerdocs#ip2coordinates which looks very interesting to me because it seems to also add the ability to use address information. I actually have address information for the server in it's database record on mysql. In other words, I could use it's current public IP and it's address to feed to this service.
The service must allow free use, even for commercial use. The server will only ever connect once to get it's geo lat/lon unless it has been moved. If it has been moved, it's db lat/lon settings will be defaulted back to 0.00000000 so the code is run again.

This is the next part of the question, I need code to complete this.

The above code connects to a php app which in turn reads/writes to the database. I need to add code to the bash function above which will check to see if lat/lon are 0.00000000. If so, then run the function, if anything else, such as it's already been set, then do not run the function and continue the script.

Currently, my code only sets and doesn't know if the lat/lon was already set. Here is the php side.

                elseif ($_POST['function'] === 'set_geo') {
                                $sql = 'UPDATE clients SET
                                geo_lat = "' . $_POST['geo_lat'] . '", geo_lon = "' . $_POST['geo_lon'] . '" WHERE id = ' . $clientid ;
                                echo "\n SQL: $sql";
                                mysql_query($sql) or die('Query failed: ' . mysql_error());
                 }

Open in new window


What I need are to check the record of this clientid, see if it's lat/lon have been set. If set, those records will contain lat/lon coordinates but if not set, they will have 0.00000000 0.00000000 which means, let's set the coordinates.

NOTES
CURLSEC is a variable which contains the entire curl connection string to authenticate and communicate with php. The script already knows it's name/password.

The $clientid is determined by php so that the proper record will be chosen when the server connects to php.

In other words, authentication and figuring out which record to write to are already in the code and not needed.
0
Comment
Question by:projects
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40390834
The service must allow free use, even for commercial use...
Really?  Why?  If the data has any economic value to you, you might consider the value versus the (usually modest) cost of using a professional service.

I've used MaxMind with good results.  Some of their data is free and some is paid.  The paid service is better (you get what you pay for) but you might be satisfied with the free data.   This article describes the way to consume MaxMind data,
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_3437-IP-Address-to-Country-in-PHP.html
0
 

Author Comment

by:projects
ID: 40391170
You are making assumptions. I give back in other ways and when I start using something on a regular basis, I usually always contribute. Not sure what that has to do with the question however as this is simply what I need at this time.

Also, mine is a two part question, one being the service, second being that I need someone to share some code with me :)
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40391756
According to this link, their data is from Maxmind.

Without access to your test data we really can't share dependable code, but this is probably something to help you get started.  Obviously it's untested, but I think it's more or less correct in principle.  I would highly recommend that you get this code off the MySQL extension, since it's deprecated and going away.  There are warning messages all over the PHP.net site.  Every day brings us one day closer to the day that PHP eliminates it, and it's just not worth keeping obsolete habits that put our scripts at risk.

elseif ($_POST['function'] == 'set_geo') 
{
    // ESCAPE THE EXTERNAL DATA TO MAKE IT SAFE FOR USE IN A QUERY
    $id  = mysql_real_escape_string($clientid);
    $lat = mysql_real_escape_string($_POST['geo_lat']);
    $lon = mysql_real_escape_string($_POST['geo_lon']);
    
    // SELECT THE ROW TO SEE IF IT NEEDS TO BE UPDATED
    $sql = "SELECT geo_lat FROM clients WHERE id = '$id' AND geo_lat = 0.0 LIMIT 1";
    $res = mysql_query($sql) or trigger_error("FAIL: $sql " . mysql_error());
    $num = mysql_num_rows($res);
    
    // IF THE CLIENT ROW HAS A ZERO LATITUDE, IT WILL BE IN THE RESULTS SET
    if ($num)
    {
        $sql = "UPDATE clients SET get_lat = '$lat', geo_lon = '$lon' WHERE id = '$id' LIMIT 1";
        $res = mysql_query($sql) or trigger_error("FAIL: $sql " . mysql_error());
    }
}

Open in new window

Best of luck with the project, ~Ray
0
 

Author Comment

by:projects
ID: 40393010
I can't take a chance on code that 'might' work since the script is in production :). I am happy to test it since it's just a function so should not break anything.

Not sure what you need by test data, there isn't any and is why I am trying to put this together so that I can have that. Right now, the code is only what you see.


As for deprecated, thank for mentioning it each time you reply to questions I post because I need to remember to get this final part updated at some point. This will be done last however.
0
 

Author Comment

by:projects
ID: 40393382
I would need to know what to put in the bash script as well to run or don't run with a new, more reliable free service.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Utilizing an array to gracefully append to a list of EmailAddresses
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now