GEO lon/lat reliable server and code needed

Posted on 2014-10-19
Last Modified: 2014-10-22
Currently, I am using 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 - | 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 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.

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.
Question by:projects
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 110

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,

Author Comment

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 :)
LVL 110

Accepted Solution

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 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') 
    $id  = mysql_real_escape_string($clientid);
    $lat = mysql_real_escape_string($_POST['geo_lat']);
    $lon = mysql_real_escape_string($_POST['geo_lon']);
    $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 ($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

Author Comment

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.

Author Comment

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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

752 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