This is an old revision of the document!


Tracking Users

Process:

Connect to database
Search for user, using their unique id

If no user was found, then add them to the database and search again

Extract user details so we can use them

And the actual code:

<?php
 
$latitude = $_GET['lat'] + 0.00003;
$longitude = $_GET['lon'];
$userRef = $_GET['userId'];
 
$userID = -1;
$userST = 0;
$userT = 0;
$userHP = 0;
 
 
include 'dbinfo.php';
 
// Search for existing user
$sql = $db->prepare( "SELECT id, st, t, hp FROM players WHERE ref=:userRef" );
$sql->bindParam( ':userRef', $userRef, PDO::PARAM_STR);
$sql->execute();
 
// How many were found (should be 1 or 0)
$count = $sql->rowCount();
 
// First check
if ($count == 0){
	// Not found. Create an entry for the user
	$sql = $db->prepare( "INSERT INTO players (ref, st, t, hp) VALUES (:userRef, 6, 6, 30)" );
	$sql->bindParam( ':userRef', $userRef, PDO::PARAM_STR);
	$sql->execute();
 
	// Should now be added, so search and try to find them
	$sql = $db->prepare( "SELECT id, st, t, hp FROM players WHERE ref=:userRef" );
	$sql->bindParam( ':userRef', $userRef, PDO::PARAM_STR);
	$sql->execute();
 
	// How many were found (should be 1 or 0)
	$count = $sql->rowCount();
}
 
// This should always be 1, unless something went badly wrong
if ($count == 1){
	// Found. Save the player's ID for later
	$results = $sql->fetchAll(PDO::FETCH_ASSOC);
	$userID = $results[0]['id'];
	$userST = $results[0]['st'];
	$userT = $results[0]['t'];
	$userHP = $results[0]['hp'];
}

Might look a bit scary - explain.

Add something to the response to view player stats

"layer": "mmodemo",
	"actions": [
		{
			"uri": "http://www.somesite.com/stats-page.php",
			"label": "ST:'.$userST.' T:'.$userT.' HP:'.$userHP.'",
			"contentType": "text/html"
		}
	],