function is_geo($latlng) { if (!strpos($latlng, ',')) { return false; } list($lat, $lng) = explode(',', $latlng); return isValidLatitude($lat) && isValidLongitude($lng); }
//The port #. It is always 3306 $connection = mysqli_connect($host, $user, $pass, $db, $port) or die(mysql_error()); $app->get('/:latitude/:longitude', function ($latitude, $longitude) { function isValidLatitude($latitude) { if (preg_match("/^-?([1-8]?[1-9]|[1-9]0)\\.{1}\\d{1,6}\$/", $latitude)) { return true; } else { return false; } } function isValidLongitude($longitude) { if (preg_match("/^-?([1]?[1-7][1-9]|[1]?[1-8][0]|[1-9]?[0-9])\\.{1}\\d{1,6}\$/", $longitude)) { return true; } else { return false; } } if (isValidLatitude($latitude) and isValidLongitude($longitude)) { global $connection; echo "Valid Coordinates"; $sql = "INSERT INTO geoip (latitude, longitude)\n VALUES ('{$latitude}', '{$longitude}')"; if ($connection->query($sql) === TRUE) { echo " New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } } }); $app->run();