Poker Mavens - API Examples (JSON)

Updated 2014-05-30

JSON Notice

Beginning with version 4.20, the Poker Mavens API contains an option to return responses from the server in JSON format. This allows the entire result to be assigned to a single object in your code and replaces the repetitive parameter names with simple arrays. For example, the RingGamesList command could return a result like this using the legacy method:

Result=Ok
Name1=Ring Game #1
Name2=Ring Game #2
Name3=Ring Game #3

And you would access the results via an associative array like this:

$first_name = $api["Name1"];

Now if you include a JSON=Yes parameter, the result will come back like this:

{"Result":"Ok","Name":["Ring Game #1","Ring Game #2","Ring Game #3"]}

And you would access the results via an object property like this:

$first_name = $api -> Name[0];  // array index starts at zero

Most programming languages (including PHP) contain a command to convert a JSON string to an object and the Poker_API() function has been modified to do just that. The legacy system still works as before and you can still see those examples on the API_Examples2.htm page.

Introduction

The Application Programming Interface (API) for Poker Mavens can be accessed by any programming language and from any computer that can make HTTP GET/POST calls to your server's file port. The examples below use the free PHP web scripting language, universally available on most web hosting sites (Linux and Windows). You may download all of these files in zip format or copy and paste them individually from the sections below:

Security

Keep in mind that the API password is passed as one of the parameters on every call from the computer running the PHP script to the computer running the game server. Therefore you should only allow the scripts to be installed on a system that you have control over. PHP code is executed on the server side so that the source is not visible to the person running it from their browser. However, if the scripting processor somehow became disabled, the server could send the raw script back to the user. To prevent your API password from being exposed in this case, you can place it in a separate file and store it in a directory located outside of the www tree. Then it would only be accessible via the local file system and not via HTTP from the outside world.

For example, if the root of your web site is located at this path:

/home/username/www/

then you can create a sibling directory like this which will not be accessible via HTTP:

/home/username/passwords/

and then save the API.php file (which contains your API password as described below) in this directory. It can then be included from any of your PHP source files like this:

<?php
  include "/home/username/passwords/API.php";
  ...
?>

Poker_API Function

The following PHP file (API.php) will be included in the examples to follow for interfacing with the game server. The Poker_API function uses the free PHP libcurl extension to make a POST call to your game server, passing it the desired parameters, and then returns an object containing the API results. The function automatically encodes all parameters and includes the required Password and JSON parameters. You simply pass the Command and other relevant parameters to Poker_API inside an associative array.

If you get an error message indicating that curl_init() is an undefined function, then you do not have the libcurl extension installed with your PHP setup. If your web site is on a managed system, contact your site operator for assistance. If you're installing PHP locally using EasyPHP, then all you have to do is right-click the EasyPHP icon in the sytem tray, select Configuration, PHP Extension, and then make sure php_curl is checked in the extension list.

Before saving the code below to a file called API.php, change the $url variable to your actual game server address (with file port and /api path). Change the $pw variable to your actual API password. Protect your API password! See the Security section above regarding the location of the file on your server.

<?php

  $url = "http://127.0.0.1:8087/api";  // put your API path here
  $pw = "xxxxxxxxxx";                  // put your API password here

  function Poker_API($params)
  {
    global $url, $pw;
    $params['Password'] = $pw;
    $params['JSON'] = 'Yes';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($curl);
    if (curl_errno($curl)) $obj = (object) array('Result' => 'Error', 'Error' => curl_error($curl)); 
    else if (empty($response)) $obj = (object) array('Result' => 'Error', 'Error' => 'Connection failed'); 
    else $obj = json_decode($response);
    curl_close($curl);
    return $obj;
  }

?>

System Stats

This is a very simple example (SysStats.php) that calls the SystemStats command and displays the data returned, including the current number of logins, filled seats, occupied tables, and server up time.

<!DOCTYPE html>
<html>
<body>
<?php
  include "API.php";
  $params = array("Command" => "SystemStats");
  $api = Poker_API($params);
  echo "<h3>Poker Server Status</h3>\r\n";
  if ($api -> Result == "Error") die("Error: " . $api -> Error);
  echo "Logins: " . $api -> Logins . "<br/>\r\n";
  echo "Filled Seats: " . $api -> FilledSeats . "<br/>\r\n";
  echo "Occupied Tables: " . $api -> OccupiedTables . "<br/>\r\n";
  echo "Up Time: " . $api -> UpTime . "<br/>\r\n";
?>
</body>
</html>

Chip Leaders

This example (ChipLeaders.php) displays the Top 10 chip leaders on your site.

<!DOCTYPE html>
<html>
<body>
<?php

  $topcount = 10;  // display top 10, use any number you like

  include "API.php";

  // Fetch the Site Name using the SystemGet API command.

  $params = array("Command" => "SystemGet", "Property" => "SiteName");
  $api = Poker_API($params);
  $result = $api -> Result;
  if ($result == "Error") die("Error: " . $api -> Error);
  $sitename = $api -> Value;

  // Fetch the list of players using the AccountsList API command.

  $params = array("Command" => "AccountsList", "Fields" => "Player,Balance");
  $api = Poker_API($params);
  $result = $api -> Result;
  if ($result == "Error") die("Error: " . $api -> Error);

  // Iterate through the players in the response and create a associative
  // chips array keyed on player name.

  $accounts = $api -> Accounts;
  $chips = array();
  for ($i = 0; $i < $accounts; $i++)
  {
    $player = $api -> Player[$i];
    $chips[$player] = $api -> Balance[$i];
  }

  // Sort array in decending order.

  arsort($chips);

  // Display results in an html table.

  echo "<table border='1' cellpadding='5'>\r\n";
  echo "<tr><th colspan='3'>$sitename - Chip Leaders</th></tr>\r\n";
  echo "<tr><th>Rank</th><th>Player</th><th>Balance</th></tr>\r\n";
  $rank = 0;
  foreach ($chips as $p => $c)
  {
    $rank++;
    echo "<tr><td>$rank</td><td>$p</td><td>$c</td></tr>\r\n";
    if ($rank == $topcount) break;
  }
  echo "</table><br>\r\n";

?>
</body>
</html>

Create New Account

This is a PHP example (NewAccount.php) for creating new accounts using a web site interface. It even graphically displays the list of available avatars (set the two $avatar___ variables to match your info):

<!DOCTYPE html>
<html>
<body>
  
  <?php

    $avatarurl = "http://127.0.0.1:8087/Image?Name=Avatars";   // set your url here
    $avatarmax = 64;       // number of avatars available

    include "API.php";

    if (isset($_REQUEST["Submit"]))
    {
      $Player = $_REQUEST["Player"];
      $RealName = $_REQUEST["RealName"];
      $Gender = $_REQUEST["Gender"];
      $Location = $_REQUEST["Location"];
      $Password1 = $_REQUEST["Password1"];
      $Password2 = $_REQUEST["Password2"];
      $Email = $_REQUEST["Email"];
      $Avatar = $_REQUEST["Avatar"];
      if ($Password1 <> $Password2) die("Password mismatch. Click Back Button to correct.");
      $params = array("Command"  => "AccountsAdd",
                      "Player"   => $Player,
                      "RealName" => $RealName,
                      "PW"       => $Password1,
                      "Location" => $Location,
                      "Email"    => $Email,
                      "Avatar"   => $Avatar,
                      "Gender"   => $Gender,
                      "Chat"     => "Yes",
                      "Note"     => "Account created via API");
      $api = Poker_API($params);
      if ($api -> Result == "Ok") echo "Account successfully created for $Player";
      else echo "Error: " . $api -> Error . "<br>Click Back Button to correct.";
      exit;
    }
  ?>

  <h3>Create New Account</h3>
  <form method="post">
    <table>
    <tr><td>Your player name:</td><td><input type="text" name="Player" /></td></tr>
    <tr><td>Your real name:</td><td><input type="text" name="RealName" /></td></tr>
    <tr><td>Your gender:</td><td><input type="radio" name="Gender" Value="Male" checked>Male &nbsp; 
                 <input type="radio" name="Gender" Value="Female">Female</td></tr>
    <tr><td>Your location:</td><td><input type="text" name="Location" /></td></tr>
    <tr><td>Select a password:</td><td><input type="password" name="Password1" /></td></tr>
    <tr><td>Confirm password:</td><td><input type="password" name="Password2" /></td></tr>
    <tr><td>Your email address:</td><td><input type="text" name="Email" /></td></tr>
    <tr><td>Your avatar:</td><td>
    <div style="width: 100px; height: 175px; overflow: auto; border: solid 2px">
      <?php
        for ($i = 0; $i < $avatarmax; $i++)
        {
          $a = "display: inline-block; width: 48px; height: 48px; background: " . 
               "url('" . $avatarurl . "') no-repeat -" . ($i * 48) . "px 0px;"; 
          $s = "<input type='radio' name='Avatar' value='" . ($i + 1) . "'";
          if ($i == 0) $s .= " checked";
          $s .= ">"; 
          $s .= "<div style=\"" . $a . "\"></div>";
          echo $s . "<br><br>\r\n";
        }
      ?>
    </div>
    </td></tr>
    </table>
    <input type="submit" name="Submit" value="Submit" />
  </form>

</body>
</html>

Export Accounts

This is a PHP example (AccountsCSV.php) for exporting the player accounts list to a comma-separated-values (CSV) file which can then be imported directly into a spreadsheet application:

<!DOCTYPE html>
<html>
<body>

<?php

  include "API.php";

  // Fetch the list of players using the AccountsList API command.

  $params = array("Command" => "AccountsList",
                  "Fields" => "Player,Title,Level,RealName,Gender,Location,Email,Balance," .
                              "RingChips,Avatar,AvatarFile,Logins,LastReset,FirstLogin," .
                              "LastLogin,ValCode,Chat,ChatColor1,ChatColor2,Custom,Note");
  $api = Poker_API($params);
  $result = $api -> Result;
  if ($result == "Error") die("Error: " . $api -> Error);

  // iterate through list

  echo "<pre>\r\n";
  echo '"Player","Title","Level","RealName","Gender","Location","Email",' .
       '"Balance","RingChips","Avatar","AvatarFile","Logins","LastReset","FirstLogin",' .
       '"LastLogin","ValCode","Chat","ChatColor1","ChatColor2","Custom","Note"' . "\r\n";
  for ($i = 0; $i < $api -> Accounts; $i++)
  {
    echo '"' . $api -> Player[$i] . '",';
    echo '"' . $api -> Title[$i] . '",';
    echo '"' . $api -> Level[$i] . '",';
    echo '"' . $api -> RealName[$i] . '",';
    echo '"' . $api -> Gender[$i] . '",';
    echo '"' . $api -> Location[$i] . '",';
    echo '"' . $api -> Email[$i] . '",';
    echo '"' . $api -> Balance[$i] . '",';
    echo '"' . $api -> RingChips[$i] . '",';
    echo '"' . $api -> Avatar[$i] . '",';
    echo '"' . $api -> AvatarFile[$i] . '",';
    echo '"' . $api -> Logins[$i] . '",';
    echo '"' . $api -> LastReset[$i] . '",';
    echo '"' . $api -> FirstLogin[$i] . '",';
    echo '"' . $api -> LastLogin[$i] . '",';
    echo '"' . $api -> ValCode[$i] . '",';
    echo '"' . $api -> Chat[$i] . '",';
    echo '"' . $api -> ChatColor1[$i] . '",';
    echo '"' . $api -> ChatColor2[$i] . '",';
    echo '"' . $api -> Custom[$i] . '",';
    echo '"' . $api -> Note[$i] . '"';
    echo "\r\n";
  }
  echo "</pre>\r\n";

?>

</body>
</html>

Player Search

This is a PHP example (PlayerSearch.php) that searches for a player via web interface. It displays whether a particular player is currently logged in or not and displays the list of ring game and tournament tables where they are waiting or seated:


<!DOCTYPE html>
<html>
<body>

  <?php
    if (isset($_REQUEST["Search"]))   // page load via search button
    {
      include "API.php";
  
      // get player name submitted by form

      $player = $_REQUEST["Player"];        
      if ($player == "")
      {
        echo "<b>No player name entered. Click the back button.</b><br/>";
        exit;
      } 

      // get list of connections 

      $params = array("Command" => "ConnectionsList", "Fields" => "Player");
      $api = Poker_API($params);
      if ($api -> Result == "Error")
      {
        echo "Error: " . $api -> Error . "<br/>";
        exit;
      }

      // loop thru connections list looking for player

      $found = false; 
      $connections = $api -> Connections;
      for ($i = 0; $i < $connections; $i++)        
      {
        if (strcasecmp($player, $api -> Player[$i]) == 0)
        {
          $found = true;
          break;
        }
      }
      echo "<b>" . $player . " is ";
      if ($found != true) echo "not ";
      echo "logged in</b><br/>";

      // get list of ring games

      echo "<br/><b>Ring Games</b><br/>";
      $params = array("Command" => "RingGamesList", "Fields" => "Name");
      $api = Poker_API($params);
      if ($api -> Result == "Error")
      {
        echo "Error: " . $api -> Error . "<br/>";
        exit;
      }

      // loop thru ring games list looking for player

      $found = 0;
      $rcount = $api -> RingGames;
      for ($i = 0; $i < $rcount; $i++)
      {

        $r = $api -> Name[$i];

        // fetch waiting
   
        $params = array("Command" => "RingGamesWaiting", "Name" => $r);
        $api2 = Poker_API($params);
        $pcount = $api2 -> Count;

        // loop thru waiting list

        for ($j = 0; $j < $pcount; $j++)
        {
          if (strcasecmp($player, $api2 -> Wait[$j]) == 0)
          {
            $found++;
            echo "In waiting list for " . $r . "<br/>";
            break;
          }
        }

        // fetch players
 
        $params = array("Command" => "RingGamesPlaying", "Name" => $r);
        $api2 = Poker_API($params);
        $pcount = $api2 -> Count;

        // loop thru players list

        for ($j = 0; $j < $pcount; $j++)
        {
          if (strcasecmp($player, $api2 -> Player[$j]) == 0)
          {
            $found++;
            echo "Seated at " . $r . "<br/>";
            break;
          }
        }
      }
      if ($found == 0) echo "(none)<br/>";

      // get list of tournaments

      echo "<br/><b>Tournaments</b><br/>";
      $params = array("Command" => "TournamentsList", "Fields" => "Name");
      $api = Poker_API($params);
      if ($api -> Result == "Error")
      {
        echo "Error: " . $api -> Error . "<br/>";
        exit;
      }

      // loop thru tournaments list looking for player

      $found = 0;
      $tcount = $api -> Tournaments;
      for ($i = 0; $i < $tcount; $i++)
      {

        $t = $api -> Name[$i];

        // fetch waiting

        $params = array("Command" => "TournamentsWaiting", "Name" => $t);
        $api2 = Poker_API($params);
        $pcount = $api2 -> Count;

        // loop thru waiting list

        for ($j = 0; $j < $pcount; $j++)
        {
          if (strcasecmp($player, $api2 -> Wait[$j]) == 0)
          {
            $found++;
            echo "In waiting list for " . $t . "<br/>";
            break;
          }
        }

        // fetch players  

        $params = array("Command" => "TournamentsPlaying", "Name" => $t);
        $api2 = Poker_API($params);
        $pcount = $api2 -> Count;

        // loop thru players list

        for ($j = 0; $j < $pcount; $j++)
        {
          if (strcasecmp($player, $api2 -> Player[$j]) == 0)
          {
            $found++;
            echo "Seated at " . $t . " - Table " . $api2 -> Table[$j] . "<br/>";
            break;
          }
        }
      }
      if ($found == 0) echo "(none)<br/>";
      echo "<br/><b>Click the back button for another search</b><br/>";
      exit;
    }
  ?>

  <!-- display input form on first load  -->
  <!-- post results back to same page -->

  <h3>Search for Player</h3>
  <form method="post">
    Screen name:<br/>
    <input type="text" name="Player" />
    <input type="submit" name="Search" value="Search" />
  </form>

</body>
</html>

Tournament Results

This is a PHP example (TourneyResults.php) that displays a droplist of tournament result files and then retrieves the one selected:


<!DOCTYPE html>
<html>
<body>
<?php

  include "API.php";

  if (isset($_REQUEST["submit"]))  // page loaded via submit button
  {

    // split out date and tourney name from selected value

    $tourney = stripslashes($_REQUEST["tourney"]);
    $trdate = Substr($tourney, 0, 10);
    $trname = Substr($tourney, 12);

    echo $tourney . "<br/>";

    // retrieve file

    $params = array("Command" => "TournamentsResults", "Date" => $trdate, "Name" => $trname);
    $api = Poker_API($params);

    // check for error

    if ($api -> Result == "Error") die($api -> Error);

    // display results

    echo "<pre>\n";
    for ($i = 0; $i < count($api -> Data); $i++) echo $api -> Data[$i] . "\n";
    echo "</pre>\n"; 
  }
  else   // initial page load
  {

    // retrieve list of files

    $params = array("Command" => "TournamentsResults");
    $api = Poker_API($params);

    // check for error

    if ($api -> Result == "Error") die($api -> Error);

    // fill droplist with dates and tourney names

    $count = $api -> Files;
    echo "<h3>Tournament Results</h3>\n";
    echo "<form method='post'>\n";
    echo "<select name='tourney'>\n";
    for ($i = 0; $i < $count; $i++)
    {
      $tr = $api -> Date[$i] . "  " . htmlspecialchars($api -> Name[$i], ENT_QUOTES);
      echo "<option value='" . $tr . "'>" . $tr . "</option>\n";
    } 

    // submit button reloads page with selected file info

    echo "</select> &nbsp; <input type='submit' name='submit' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p>\n";
  }

?>
</body>
</html>

Hand History

This is a PHP example (HandHistory.php) that displays a droplist of hand history files and then retrieves the one selected:


<!DOCTYPE html>
<html>
<body>
<?php

  include "API.php";

  if (isset($_REQUEST["submit"]))  // page loaded via submit button
  {

    // split out date and table name from selected value

    $history = stripslashes($_REQUEST["history"]);
    $hhdate = Substr($history, 0, 10);
    $hhname = Substr($history,12);  

    // retrieve file

    $params = array("Command" => "LogsHandHistory", "Date" => $hhdate, "Name" => $hhname);
    $api = Poker_API($params);

    // check for error

    if ($api -> Result == "Error") die($api -> Error);

    // display results

    echo "<pre>\n";
    for ($i = 0; $i < count($api -> Data); $i++) echo $api -> Data[$i] . "\n";
    echo "</pre>\n"; 
  }
  else   // initial page load
  {

    // retrieve list of files

    $params = array("Command" => "LogsHandHistory");
    $api = Poker_API($params);

    // check for error

    if ($api -> Result == "Error") die($api -> Error);

    // fill droplist with dates and table names

    $count = $api -> Files;
    echo "<h3>Hand History</h3>\n";
    echo "<form method='post'>\n";
    echo "<select name='history'>\n";
    for ($i = 0; $i < $count; $i++)
    {
      $hh = $api -> Date[$i] . "  " . htmlspecialchars($api -> Name[$i], ENT_QUOTES);
      echo "<option value='" . $hh . "'>" . $hh . "</option>\n";
    } 

    // submit button reloads page with selected file info

    echo "</select> &nbsp; <input type='submit' name='submit' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p>\n";
  }

?>
</body>
</html>

Error and Event Logs

This is a PHP example (Logs.php) that displays a droplist of error log and event log files and then retrieves the one selected:

<!DOCTYPE html>
<html>
<body>
<?php

  include "API.php";

  if (isset($_REQUEST["errors"]))  // retrieve selected error log
  {
    $edate = $_REQUEST["edate"];
    $params = array("Command" => "LogsError", "Date" => $edate);
    $api = Poker_API($params);
    if ($api -> Result == "Error") die($api -> Error);
    echo "<pre>\n";
    for ($i = 0; $i < count($api -> Data); $i++) echo $api -> Data[$i] . "\n";
    echo "</pre>\n"; 
  }
  elseif (isset($_REQUEST["events"]))  // retrieve selected event log
  {
    $edate = $_REQUEST["edate"];
    $params = array("Command" => "LogsEvent", "Date" => $edate);
    $api = Poker_API($params);
    if ($api -> Result == "Error") die($api -> Error);
    echo "<pre>\n";
    for ($i = 0; $i < count($api -> Data); $i++) echo $api -> Data[$i] . "\n";
    echo "</pre>\n"; 
  }
  else  // initial page load - display selection droplists
  {

    // retrieve error log dates

    $params = array("Command" => "LogsError");
    $api = Poker_API($params);
    if ($api -> Result == "Error") die($api -> Error);
    $count = $api -> Files;
    echo "<h3>Error Logs</h3>\n";

    // display droplist of dates

    echo "<form method='post'>\n";
    echo "<select name='edate'>\n";
    for ($i = 0; $i < $count; $i++)
    {
      $e = $api -> Date[$i];
      echo "<option value='" . $e . "'>" . $e . "</option>\n";
    } 

    // submit button reloads page with selected error log date

    echo "</select> &nbsp; <input type='submit' name='errors' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p><br/>\n";


    // retrieve event log dates

    $params = array("Command" => "LogsEvent");
    $api = Poker_API($params);
    if ($api -> Result == "Error") die($api -> Error);
    $count = $api -> Files;
    echo "<h3>Event Logs</h3>\n";

    // display droplist of dates

    echo "<form method='post'>\n";
    echo "<select name='edate'>\n";
    for ($i = 0; $i < $count; $i++)
    {
      $e = $api -> Date[$i];
      echo "<option value='" . $e . "'>" . $e . "</option>\n";
    } 

    // submit button reloads page with selected event log date

    echo "</select> &nbsp; <input type='submit' name='events' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p>\n";
  }

?>
</body>
</html>

Login Stats

This is a PHP example (LoginStats.php) that parses through one or more event logs and calculates login statistics. Specifically, for each period of server up time, it displays the total number of logins, unique logins, and peak logins. The total logins is just a simple count of logins. If a single player logged in and out 10 times then 10 is added to that count. The unique count is the number of different players that logged in at least once during that time. The peak count is the maximum number of players that were all logged in at the same time. Note: if you see a "?" in the Start or Stop column of the output, that means the server went online or offline outside of the date range selected (a new log file is started when the clock passes midnight). In your log settings, you must have "Log system events", "Log login events", and "Log logout events" all set to "Yes" to use this function.


<!DOCTYPE html>
<html>
<body>
<?php

  include "API.php";

  // retrieve event log dates into $edates array

  $params = array("Command" => "LogsEvent");
  $api = Poker_API($params);
  if ($api -> Result == "Error") die($api -> Error);
  $count = $api -> Files;
  $edates = array();
  for ($i = 0; $i < $count; $i++) $edates[] = $api -> Date[$i];
 
  if (isset($_REQUEST["stats"]))  // parse selected event logs
  {

    function addrecord()  // add a record representing a server session
    {
      global $playerlist, $starttime, $stoptime, $logins, $peaklogins;
      $unique = count($playerlist); 
      echo "<tr><td>$starttime</td><td>$stoptime</td><td>$logins</td><td>$unique</td><td>$peaklogins</td></tr>\n";
    }

    function resetstats()  // new session
    {
      global $playerlist, $starttime, $stoptime, $logins, $currentlogins, $peaklogins;
      $logins = 0;
      $currentlogins = 0; 
      $peaklogins = 0;
      $playerlist = array();
      $starttime = "?";
      $stoptime = "?";
    }

    $edate1 = $_REQUEST["edate1"];
    $edate2 = $_REQUEST["edate2"];
    if ($edate1 > $edate2) die("Starting date is greater than ending date. Click the Back button.");

    // loop through event files and merge into $events array

    $events = array(); 
    for ($i = $edate1; $i <= $edate2; $i++)
    {
      $params = array("Command" => "LogsEvent", "Date" => $edates[$i]);
      $api = Poker_API($params);
      if ($api -> Result == "Error") die($api -> Error);
      $events = array_merge($events, $api -> Data);
    }

    resetstats();
    $title = "Logins " . $edates[$edate1];
    if ($edate1 != $edate2) $title = $title . " to " . $edates[$edate2];
    echo "<table cellpadding='5' border='1'>\n";
    echo "<tr><th colspan='5'>$title</th></tr>\n";
    echo "<tr><th>Start</th><th>Stop</th><th>Logins</th><th>Unique</th><th>Peak</th></tr>\n";

    // loop through event lines

    foreach ($events as $line)
    {
      if (strpos($line,"System|Game server started") !== false)
      {
        $starttime = substr($line,0,19);
      }
      elseif (strpos($line,"System|Game server stopped") !== false)
      {
        $stoptime = substr($line,0,19);
        addrecord();
        resetstats();
      }
      elseif (strpos($line," logged into session ") !== false)
      {
        $logins++;
        $currentlogins++;
        if ($currentlogins > $peaklogins) $peaklogins = $currentlogins;
        $p = strpos($line," logged into session ");
        $player = substr($line,26,$p-26);
        $playerlist[$player] = true;
      }
      elseif (strpos($line," logged out") !== false)
      {
        if ($currentlogins > 0) $currentlogins--;
      }
    }
    if ($starttime != "?") addrecord();
    echo "<tr><th colspan='5'>Records parsed: " . count($events) . "</th></tr>\n";
    echo "</table><br/>\n";
  }
  else  // initial page load - display selection droplists
  {

    echo "<h3>Login Stats</h3>\n";
    echo "<p>Event files found: " . $count . "</p>\n";

    // display droplist of dates

    echo "<form method='post'>\n";
    echo "Start date: <select name='edate1'>\n";
    foreach ($edates as $i => $e) echo "<option value='" . $i . "'>" . $e . "</option>\n";
    echo "</select> &nbsp; ";

    echo "End date: <select name='edate2'>\n";
    foreach ($edates as $i => $e) echo "<option value='" . $i . "'>" . $e . "</option>\n";
    echo "</select> &nbsp; ";

    // submit button reloads page with selected event log dates

    echo "<input type='submit' name='stats' value='Submit'>\n";
    echo "</form>\n";
  }

?>
</body>
</html>

Automated Logins

These are various examples that show how to automate logins from an external web page. This first one (Login.htm) is just a simple HTML form (no scripting) where the user inputs their own login name and password and it is forwarded to the player client directly. Replace the URL in the action parameter with your own. Remove the target parameter if you want the poker client to load into the current window instead of spawning a new one.

<!DOCTYPE html>
<html>
<body>
<h3>Poker Login</h3>
<form method="post" action="http://127.0.0.1:8087" target="_blank">
  <table>
    <tr>
      <td>Player Name:</td>
      <td><input type="text" name="LoginName"></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input type="password" name="LoginPassword"></td>
    </tr>
    <tr>
      <th colspan="2"><input type="submit" value="Login"></th>
    </tr>
  </table>
</form>
</body>
</html>

The following example (SessionKey1.php) will verify the player's password, generated a session key, and then log that player in using Javascript and the window.location.assign() function.

<!DOCTYPE html>
<html>
<body>
<?php

  $server = "http://127.0.0.1:8087";   // set your site url here
  include "API.php";

  if (isset($_POST["Login"]))
  {
    $player = $_POST["Player"];
    $password = $_POST["Password"];
    $params = array("Command" => "AccountsPassword", "Player" => $player, "PW" => $password);
    $api = Poker_API($params);
    if ($api -> Result != "Ok") die($api -> Error . "<br/>" . "Click Back Button to retry.");
    if ($api -> Verified != "Yes") die("Password is incorrect. Click Back Button to retry.");
    $params = array("Command" => "AccountsSessionKey", "Player" => $player);
    $api = Poker_API($params);
    if ($api -> Result != "Ok") die($api -> Error . "<br/>" . "Click Back Button to retry.");
    $key = $api -> SessionKey;
    $src = $server . "/?LoginName=" . $player . "&SessionKey=" . $key;
    echo "<script>window.location.assign('$src')</script>\r\n</body>\r\n</html>";
    exit;
  }
?>

  <h3>Poker Login</h3>
  <form method="post">
    <table>
      <tr>
        <td>Player Name:</td>
        <td><input type="text" name="Player"></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="Password"></td>
      </tr>
      <tr>
        <th colspan="2"><input type="submit" name="Login" value="Login"></th>
      </tr>
    </table>
  </form>

</body>
</html>

The next example (SessionKey2.php) is similar to the previous one except it creates an iframe for the client instead of using Javascript.

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
  div { position: absolute; top: 0px; right: 0px; left: 0px; bottom: 0px; }
  iframe { width: 100%; height: 100%; border: none; }
  body, html { margin: 0; padding: 0; overflow: hidden; }
</style>
</head>
<body>
<?php

  $server = "http://127.0.0.1:8087";   // set your site url here
  include "API.php";

  if (isset($_POST["Login"]))
  {
    $player = $_POST["Player"];
    $password = $_POST["Password"];
    $params = array("Command" => "AccountsPassword", "Player" => $player, "PW" => $password);
    $api = Poker_API($params);
    if ($api -> Result != "Ok") die($api -> Error . "<br/>" . "Click Back Button to retry.");
    if ($api -> Verified != "Yes") die("Password is incorrect. Click Back Button to retry.");
    $params = array("Command" => "AccountsSessionKey", "Player" => $player);
    $api = Poker_API($params);
    if ($api -> Result != "Ok") die($api -> Error . "<br/>" . "Click Back Button to retry.");
    $key = $api -> SessionKey;
    $src = $server . "/?LoginName=" . $player . "&amp;SessionKey=" . $key;
    echo "<div><iframe src='$src'></iframe></div>\r\n</body>\r\n</html>";
    exit;
  }
?>

  <h3>Poker Login</h3>
  <form method="post">
    <table>
      <tr>
        <td>Player Name:</td>
        <td><input type="text" name="Player"></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="Password"></td>
      </tr>
      <tr>
        <th colspan="2"><input type="submit" name="Login" value="Login"></th>
      </tr>
    </table>
  </form>

</body>
</html>