GPS Contact Tracing With PHP MySQL – A BIG NO!

Unless you have been living under a rock, we all know what happened in 2020 – A global pandemic. Then, interestingly, I got quite a number of questions from students worldwide on how to do contact tracing using GPS.

That only won an instant facepalm from this code ninja, and no, I did not reply for their own good. If you are one of those students, give it up, and find a better project idea. Why? Here is a tutorial that is different from my usual ones, one that some lazy lecturers didn’t bother to teach – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

TABLE OF CONTENTS

Download & Notes Proof Of Concept Why No Good?
The Lesson The End

 

 

DOWNLOAD & NOTES

First, here is the download link to the example source code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

  • Create a database and import 1-tracing.sql.
  • Change the database settings in 2-tracing.php to your own.
  • Launch 3-tracing.html in your browser.
  • Access permission to the GPS is required. Use http://, not file://.

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

PROOF OF CONCEPT – THE CODE

Is it possible to track a person using GPS? Of course, the technology is already out there – Live postal tracking, delivery rider tracking, running route and distance trackers, etc… We only have to reuse it in another way. Here’s a ripoff from my GPS tracking tutorial.

 

DATABASE TABLE

1-tracing.sql
CREATE TABLE `tracing` (
  `trace_id` int(11) NOT NULL,
  `trace_time` datetime NOT NULL DEFAULT current_timestamp(),
  `trace_lat` decimal(10,6) NOT NULL,
  `trace_lng` decimal(10,6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `tracing`
  ADD PRIMARY KEY (`trace_id`,`trace_time`);ODE

Yes, it’s that simple. All we need to track a person’s movements are 4 fields:

  • trace_id To identify who-is-who, the person’s unique ID.
  • trace_time The time when the location is recorded.
  • trace_lat and trace_lng The latitude and longitude.

 

SERVER-SIDE SCRIPT

2-tracing.php
<?php
// (A) DATABASE SETTINGS
// ! CHANGE TO YOUR OWN !
define('DB_HOST', 'localhost');
define('DB_NAME', 'test');
define('DB_CHARSET', 'utf8');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
 
// (B) CONNECT TO DATABASE
try {
  $pdo = new PDO(
    "mysql:host=" . DB_HOST . ";charset=" . DB_CHARSET . ";dbname=" . DB_NAME,
    DB_USER, DB_PASSWORD
  );
} catch (Exception $ex) {
  print_r($ex);
  die();
}

// (C) INSERT COORDINATES
try {
  $stmt = $pdo->prepare("INSERT INTO `tracing` (`trace_id`, `trace_lat`, `trace_lng`) VALUES (?,?,?)");
  $stmt->execute([$_POST['id'], $_POST['lat'], $_POST['lng']]);
} catch (Exception $ex) {
  print_r($ex);
  die();
}
 
// (D) DONE - CLOSE CONNECTION
$stmt = null;
$pdo = null;
echo "OK";

Here’s the handling server-side script. Simply POST the person’s ID and coordinates to save a new location entry.

 

 

CLIENT-SIDE JAVASCRIPT

3-tracing.html
// (A) TRACKING
var track = {
  // (A1) TRACKING OPTIONS
  id : 999, // Person ID
  delay : 2000, // Delay in between each position update, in milliseconds

  // (A2) PERIODIC UPDATE
  update : function () {
    navigator.geolocation.getCurrentPosition(function (pos) {
      // DATA
      var data = new FormData();
      data.append('id', track.id);
      data.append('lat', pos.coords.latitude);
      data.append('lng', pos.coords.longitude);

      // AJAX
      var xhr = new XMLHttpRequest();
      xhr.open('POST', "2-tracing.php", true);
      xhr.onload = function () { console.log(this.response); };
      xhr.send(data);
    });
  }
};

// (B) INIT ON PAGE LOAD
window.addEventListener("load", function(){
  if (navigator.geolocation) { setInterval(track.update, track.delay); }
  else { alert("NO GPS SUPPORT!"); }
});

This script just gets the GPS coordinates and sends it to the above PHP script… Actually, this can also be an app or whatever tracker device that can send data to the server.

 

 

WHY IS THIS NO GOOD?

GPS technology has already been proven to be working for donkey years. So why is that no one is adopting it for mass contact tracing!?

 

SCIENCE MATTERS

First off, what is the big idea behind contact tracing? Using the data in various ways for the prevention of the spread of disease:

  • Know where an infected person has visited before, possibly close the place down for deep cleaning if it is a crowded place.
  • Quarantine all the people who have come in close and prolonged contact with an infected person.
  • Prevent overcrowding at places.

But think harder – Tracing one or two people is not going to do any good. We will need to trace a lot of people for the data to be useful… Which will give rise to a lot more issues.

 

PRIVACY MATTERS

Yep, privacy is a huge issue in tracking. How many people will like to be tracked by GPS 24/7? Wear a GPS device that cannot be removed? Pay a fine or go to jail for losing that tracking device? If the battery goes flat for too long, the police will plaster your face all over the streets?

Isn’t that something we do for people on probation? Why are we criminalizing innocent people? Even if you are thinking of “we are just tracking a few confirmed infected cases, self-quarantined at home” – Their privacy still matters, and there are laws to prevent such tracking in some countries.

P.S. If you are still insisting that it is OK to track people using GPS, here – Apple and Google banned location tracking in contact tracing apps.

 

 

HARDWARE & COST MATTERS

All right, let’s say that some government is willing to make compromises to track people using GPS. How much time, money, and effort will be required to make this work effectively? To track a huge group of people (even if it is just the infected/suspected cases):

  • Massive amounts of GPS trackers will have to be purchased/manufactured.
  • To support that amount of tracking and data, we will also need the equivalent infrastructure – Servers, network, and personnel.
  • With that, comes along with possible security issues, the need to hire security experts.

So… Just how much money and human resources is that?

 

ENFORCEMENT MATTERS

Now, let’s just say that some government is still willing to make compromises to the privacy, law, and willing to spend on technology. Then here comes the most important question – Who and how is the enforcement going to take place?

  • Everyone must download a tracing app and turn it on?
  • People without a GPS capable device must report to a certain place, at a certain time to collect a GPS tracker?
  • How to deal with people who don’t start the tracing?
  • How to deal with people who are untraceable?
  • What to do when people break the rules?
  • How many policemen are required to enforce all of these?
  • How many resources and money is required to enforce all of these?

 

 

THE LESSON

By now, it should be clear why the world is not adopting GPS contact tracing – It is grossly expensive and ineffective. What’s the lesson that some lecturers didn’t teach? Thinking like a human and planning ahead.

 

TRACING TECHNOLOGY IS ALREADY OUT THERE

For you guys who are somehow still thinking that GPS contact tracing is a worthy investment, the technology is already in place.

  • Probation tracking system – As mentioned above, there are already GPS trackers for people on probation.
  • Bluetooth tracking – Just do a search on “open source Bluetooth tracker”.

 

THINKING FAR MATTERS

No, it’s not that GPS tracking is bad, but allow me to “officially” write this down. A project isn’t just about “this is technically possible, let’s do it”. There are many other considerations – Time, money, effort, resources, laws, enforcement, effectiveness, etc…

Also, there are alternatives to solving problems apart from using technology. Heard of social distancing? Restricting the number of people to prevent overcrowding? Educating people to wear masks and wash hands? All of these prevention measures are much cheaper and more effective. So much that it makes GPS tracking look like a gimmick.

So here is a final gentle reminder to people who are planning to do a GPS contact tracing project – No, not until you have some super creative way to make the technology work wonders.

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *