Location: PHPKode > scripts > GG-Tracker GPS and GSM Tracking > gg-tracker-gps-and-gsm-tracking/storesql.php
<?php 

header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); 
header("Pragma: no-cache"); 
header("Content-Type: text/plain; charset=ISO-8859-1");
header('Content-Disposition: inline; filename="response.txt"');

/* Include settings */
error_reporting(0);  
include ("config.php");

/* Set everything to UTC time and date */
putenv("TZ=GB/London");

/* Show time and arguments in the response to the HTTP client */
if ($debuginfo) {
	$timestamp = gmdate("D, d M Y H:i:s");
	echo $timestamp; 
	echo "\r\n"; 
	
	print_r($_GET);
	echo "\r\n";
}

// Connect and open database 
$link = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if (!$link) {
	printf("Connect failed: %s\r\n", mysqli_connect_error());
	exit();}


/* Prepare an insert statement for the GPS */
$query = "INSERT INTO GPS (PHONE,STATUS,LATITUDE,LONGITUDE,SPEED_KNOTS,COURSE_DEG,UTCTIME,UTCDATE,EVENT_TYPE,CELL_ID,LAC,MCC,MNC,NWNAME,LABEL,REMOTE_IP)"
        . " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
$stmt = mysqli_prepare($link, $query);

if (!$stmt) {
	printf("FATAL SERVER ERROR - SQL Prepare failed: %s\r\n", mysqli_error($link));
	printf("Errorcode: %d\r\n", mysqli_errno($link));
	printf("%s\r\n", mysqli_info($link));
	exit();
}

if (array_key_exists('REMOTE_ADDR', $_SERVER))
	$remoteip = $_SERVER["REMOTE_ADDR"];
else
	$remoteip = NULL;   

/* Check if phone is allowed to store data */
$imei = $_GET['imei'];
if ($cphone != $imei) {
	printf("FATAL ERROR - Phone is not allowed to store data\r\n");
	printf("Phone IMEI: ", $imei,"\r\n");
	exit();
}
    
//Define GSM Tracking variables 
$mode =$_GET['inmode'];
$cell = $_GET['incell'];
$lac = $_GET['inlac'];
$mcc = $_GET['inmcc'];
$mnc = $_GET['inmnc'];
$nwname = $_GET['innwname'];

if (empty($mode)) $mode = $_GET['mode'];
if (empty($cell)) $cell = $_GET['cell'];
if (empty($lac)) $lac = $_GET['lac'];
if (empty($mcc)) $mcc = $_GET['mcc'];
if (empty($mnc)) $mnc = $_GET['mnc'];
if (empty($nwname)) $nwname = $_GET['nwname'];

//Define GPS Tracking variables 
$status = $_GET['status'];
$lat = $_GET['lat'];
$lon = $_GET['lon'];
$speed = $_GET['speed'];
$course = $_GET['course'];
$time = $_GET['time'];
$date = $_GET['date']; 

//Check if we can clear some empty data
if (empty($time)) $time = date('His');
if (empty($date)) $date = date('dmy');

//Check if GPS is switched on, if not mark it as X and clean variables
if (empty($status)) {
	$status = "X";
	$speed = 0;
	$course = 0;
}

/* See if there is a label (preset position)available */
$lquery = 'SELECT LABEL, LATITUDE, LONGITUDE FROM CELL ' 
       . 'WHERE CELL_ID = '.$cell.' AND LAC = '.$lac.' AND MCC = '.$mcc.' AND MNC = '.$mnc.' LIMIT 1';
$lresult = mysqli_query($link, $lquery);
$row_cnt = mysqli_num_rows($lresult);

//If a row is found then label the location
if (!empty($row_cnt)) {
	$line = mysqli_fetch_array($lresult, MYSQLI_ASSOC);
	$label = $line['LABEL'];}

//If GPS is switched off and a label is found then use predefined locations  
if ((!empty($row_cnt)) && ($status == "X")) {
	$lat = $line['LATITUDE'];
	$lon = $line['LONGITUDE'];}

//If no label can be found set it to "Undefined"
if (empty($label)) $label = "Undefined";

/* Check if we can guess the location using previous GPS locations if GPS is switched off */
if (($status == "X") && ($label == "Undefined")) {

	//Get previous GPS locations of GSM Cell, use only good locations where Status = A, B, R or T
	$lquery = 'SELECT LATITUDE, LONGITUDE FROM GPS WHERE '
			. 'STATUS IN (\'A\',\'B\',\'R\',\'T\') ' 
			. 'AND CELL_ID = '.$cell.' AND LAC = '.$lac.' AND MCC = '.$mcc.' AND MNC = '.$mnc.' '
			. 'ORDER BY RAND() LIMIT 10';
	$lresult = mysqli_query($link, $lquery);

	$row_cnt = mysqli_num_rows($lresult);
	$lon_t = 0;
	$lat_t = 0;
   
	//Only calculate if threshold is met   
	if ($row_cnt >= $ant_threshold) {   
		for ($i = 1; $i <= $row_cnt; $i++) {

			$line = mysqli_fetch_array($lresult, MYSQLI_ASSOC);
			$lon = $line['LONGITUDE'];
			$lat = $line['LATITUDE'];
 
			$lon_t = $lon_t + $lon;
			$lat_t = $lat_t + $lat;
		}
	$lon = $lon_t / $row_cnt;
	$lat = $lat_t / $row_cnt;
	$speed = "";
	$course = "";
	$status = "C";
	}
	else {
	//MySQL database got no results, set status to Z
	$status = "Z";	
	}
}

/* Before we do any complicated stuff, load math functions */
include ("math_functions.php");

/* Integrate CellDB.org if allowed by user */
if ($celldb) include("celldb_org.php");

/* See if we can calculate the average speed if no GPS is attached */
if (($status == "C") || ($status == "R") || ($status == "T") || ($status == "X")) {
	
	//Retreive previous CellID that was filled
	$lquery = 'SELECT TIME_RECEIVED, LATITUDE, LONGITUDE FROM GPS '
			 .'WHERE LATITUDE IS NOT NULL AND LONGITUDE IS NOT NULL '
			 .'ORDER BY GPSMSGID DESC LIMIT 1';
	$lresult = mysqli_query($link, $lquery);
	
	$line = mysqli_fetch_array($lresult, MYSQLI_ASSOC);
	$lon2 = $line['LONGITUDE'];
	$lat2 = $line['LATITUDE'];
	$time2 = $line['TIME_RECEIVED'];
	
	//Calculate time delay between intervals in seconds 
	$int_sec = strtotime ($time2) - time();
	
	//Calculate distance between intervals in metres
	$int_dis = great_circle_distance ($lat, $lon, $lat2, $lon2) * 1000;
	
	//Calculate average speed in knots and round by 2 digits
	$speed = abs(round((($int_dis / $int_sec) * 3.6) / 1.852, 2));
}

/* Write position for Google network link */ 
if ($gnwlink) include("gnwlink.php");

/* Create RSS file for data exchange */
if ($rss) include("rsscreate.php");

/* Write data into database */
mysqli_stmt_bind_param($stmt, 'ssddddsssiiiisss', 
                              $imei, $status, $lat, $lon,
                              $speed, $course, $time, $date,
                              $mode, $cell, $lac, $mcc, $mnc, $nwname,
                              $label, $remoteip);

/* Execute the statement */
mysqli_stmt_execute($stmt);

/* close statement */
mysqli_stmt_close($stmt);

/* free results */ 
mysqli_free_result($lresult);

/* close connection */    
mysqli_close($link); 

/* Write status and possible location as output */
if (($status == "F") || ($status == "Z")) {
	echo $time.", ".$status."\r\n"."No location found";
}
else
{
	echo $time.", ".$status."\r\n".$lat.", ".$lon;
}

?>
Return current item: GG-Tracker GPS and GSM Tracking