[Release] Check with PHP if a player is ingame

Joined
Sep 10, 2006
Messages
1,243
Reaction score
183
Hello

There are 2 ways to check if a player is logged in.

1. KOSP Status Server (under R11 KOSP): checks if the requested player is ingame using the player_list command
2. Auth log: checks the last login/logout log of the account. If the last one is a login, disable access.

METHOD 1: KOSP

You can use the StatusServer in KOSP R9 and before to check if a player is logged in or not.

This following script requires basic PHP knowledge and I will NOT reply any question about PHP that is not related to this script.

PHP:
<?php
$msconnect = mssql_connect("IP","user","pass"); //db connection
$kaldb = 'kal_db'; //db name

mssql_select_db($kaldb);

function Send_KOSP_command($cmd) 
 {
 
 // this info should match with the ones in config.properties
 // make sure you set StatusServer = true
 
  $port = "20009"; 
  $host = "localhost";  
  $user = "admin";
  $pass = "password";

  $fp = @fsockopen($host, $port); 

  if($fp) 
  {    
   // Send login 
   fwrite($fp,"login $user $pass\r"); 
   // Set mode 
   fwrite($fp,"mode web\r"); 
   // Send command 
   fwrite($fp, "$cmd\r"); 
   while(!feof($fp)) 
    $result .= fread ($fp, 1024); 
   fclose($fp); 
   return $result; 
   } 
  return NULL;
 }
 
 function pid2name($string)
 {
 $sql = mssql_fetch_array(mssql_query("SELECT [Name] FROM Player WHERE PID = '".$string."'"));
 $name = $sql['Name'];
 return $name;
 }


//check if player is online
$PlayerList = Send_KOSP_command("player_list"); 
$part = explode(", ", $PlayerList);
$online = 0;
$i = 0;
while($i < count($part))
{
if($part[$i] == pid2name($_SESSION['pid'])) //you can change the variable pid2name($_SESSION['pid']) to a direct name instead of using the function. This would look like this:
//if($part[$i] == "NameHere")
{
$online = 1;
}
$i++;
}


if($online == 1)
{
echo "You must be logged out of the game when using this function!";
}
else
{
//your site content
}
?>
I use it to prevent players from using several PHP scripts on the Client Site while being logged in such as skill upgrades, item transactions as they are not live (if you delete an item in the database it will still be ingame until you relog and that could lead to abuse)

METHOD 2: Auth log

Easy, I made it when discovering the Auth Log :)

Here's how to do it with the kal_auth Log table:

Function:
PHP:
function online($uid)
{
global $kalauth;
mssql_select_db($kalauth);
$sql = mssql_fetch_array(mssql_query("SELECT TOP 1 * FROM Log WHERE Player1 = '".$uid."' ORDER BY date desc"));
$number = $sql['Type'];
return $number;
 }

Check:
PHP:
$uid = 548;
if(online($uid) == "0")
{
// Logged in
// fail message
}
else
{
// Logged out
// run query
}

Have fun with it

Greets
Bjorn
 
Last edited:
Nice one :good:
Is it working on F R10 003 055 146 0948 KOSP version ?
(I tested on that version but nothing happens. I'm getting clean php page.)
 
Back