GEO lon/lat reliable server and code needed

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.
projectsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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
projectsAuthor Commented:
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
Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
projectsAuthor Commented:
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
projectsAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.