What I want my code to do is get the user's IP address in the background, and when they fill out the field(s) and hit submit it sends not only the information they input, but their GPS coordinates as well to a database and table I already have set up.
Essentially what I am attempting to do is mark a user's current GPS location when they submit data to a database (like timestamping except with latitude and longitude obviously). To get the location information I followed a tutorial here Getting User Location Information with PHP that uses telize.com/geoip API to grab GPS coordinates from whatever IP Address accesses it.
I'm currently using two files to test it out: This one which houses my database connection information (with user and password omitted) as well as the class of functions needed to use the API:
<?php
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_USER', '');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_NAME', 'geo');
$link = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("could not connect");
if(!$link) {
die("Connection Unsuccessful");
}
class Geo
{
protected $api = 'http://www.telize.com/geoip/%s';
protected $properties = [];
public function __get($key)
{
if (isset($this->properties[$key])){
return $this->properties[$key];
}
return null;
}
public function request($ip)
{
$url = sprintf($this->api, $ip);
$data = $this->sendRequest($url);
$this->properties = json_decode($data, true);
}
protected function sendRequest($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
return curl_exec($curl);
}
}
?>
And this one which holds the user end that executes the class, receives data and stores it in the database:
<?php
//modeled from a tutorial found here: https://www.youtube.com/watch?v=Rr36E8NAORI
//contains database connection details and class to get information from IP Address
require_once 'geo2.php';
//variable to detect IP address to be used with location API below and in geo2.php
//currently $_SERVER['REMOTE_ADDR'] only displays ::1 on localhost, using example IP for testing
$ipAddress = '86.174.200.225';
echo $ipAddress;
//method of pulling specific location information from API using IP Address
$geo = new Geo;
$geo->request($ipAddress);
$lat = $geo->latitude;
$lng = $geo->longitude;
echo $lat . $lng;
if(isset($_POST['submit'])){
//get the manually inserted datum 'title'
$title = mysqli_real_escape_string($link, $_POST['title']);
// stored in a variable to TEST
$sql = "INSERT INTO geo (id, lat, lng, title) VALUES (NULL, '$lat', '$lng', '$title')";
if(!mysqli_query($link, $sql)) {
die('Error: ' . mysqli_error($link));
}
else { echo "Success"; }
}
?>
<form action="" method="post">
<input name="title" type="text" />
<input value="Submit" type="submit" />
</form>
The core issues being that upon "submit" nothing is sent to the database and I do not receive any error messages or "Success" as intended. I am trying to find out whether my solution simply is not a solution at all or if I am making a minor mistake.
I'm running all of this on WAMPSERVER 2.5, and I know it is not a matching error as the database receives inputs when I make $lat and $lng input fields like $title.