<?php
// Utility sample for Aspicore GSM Tracker
// Show URL arguments as plain text and stores data into MySQL database
//
// This sample demonstrates, how Aspicore GSM Tracker sends location data to the server via HTTP.
//
// Usage: Set "Internet page URL" in Aspicore GSM Tracker settings to point to this page.
//        You see the output in your phone in the Info tab every time the phone sends
//        some location data to the server via a HTTP connection.
//
// Note:  For simplicity, this script stores GPS data only.
//        You can amend it to store also the cell tower info.
//
// (c) Aspicore Ltd 2005, www.aspicore.com
//
// Tested with PHP 5.0.3, Windows XP, IIS 5.1, MySQL 4.1.10a-nt and with Windows Server 2003, IIS 6.0
// The mysqli extension of PHP requires version 4.1.3 or above of MySQL
//
// NB. If you are using PHP with IIS, set output_buffering = On in php.ini
// In Windows, remember to enable extension=php_mysqli.dll in php.ini
//             make also sure, that php_mysqli.dll is at location php.ini/extension_dir
//             and that libmySQL.dll is along the system path
// In Windows Server 2003, allow the required dll files in the Web Service Extensions tab
// ------------------------------
// Change History:
// 2005-04-04 jje - SQL script created

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"');

// Show time and arguments in the response to the HTTP client

$timestamp = gmdate("D, d M Y H:i:s");
echo
$timestamp;
echo
"\r\n" ;

print_r($_GET);
echo
"\r\n" ;

//------------------------
// Store new data into MySQL database
// (Create database 'gsmtrack' first with script GSM_Tracker_MySQL_tab.sql)
//------------------------

// Connecting, selecting database

// ***********************************************************************
// mysqli_connect parameters: host, username, passwd, dbname
$link = mysqli_connect("localhost", "root", "newpwd", "gsmtrack");
// ***********************************************************************

/* check connection */
if (!$link) {
   
printf("Connect failed: %s\r\n", mysqli_connect_error());
   exit();
}

//echo 'Connected successfully';

/* Prepare an insert statement */

$query = "INSERT INTO GPS (PHONE,STATUS,LATITUDE,LONGITUDE,SPEED_KNOTS,COURSE_DEG,UTCTIME,UTCDATE,LABEL,REMOTE_IP)"
        
. " VALUES (?,?,?,?,?,?,?,?,?,?)";
$stmt = mysqli_prepare($link, $query);

/* check prepare result */
if (!$stmt) {
   
/* Show ERROR INFORMATION to the HTTP client */
   
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));
   
printf("Check that your database structure matches GSM_Tracker_MySQL_tab.sql\r\n");
   
printf("Check access privileges (SQL GRANT statement).\r\n");
   
printf("\r\nTried to prepare the following SQL statement for execution:\r\n");
   
printf("$query\r\n");
   
printf("\r\nYour MySQL Server version is %s\r\n", mysqli_get_server_info($link));
   
printf("Your PHP version is %s\r\n", PHP_VERSION);
   exit();
}

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


mysqli_stmt_bind_param($stmt, 'ssddddssss',
                              
$_GET['imei'], $_GET['status'], $_GET['lat'], $_GET['lon'],
                              
$_GET['speed'],$_GET['course'], $_GET['time'], $_GET['date'],
                              
$_GET['label'],$remoteip);

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

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

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

echo
"Row added into DB\r\n";

//------------------------
?>