Get server info(player count and map name) in html

Hello everyone.
First, sorry for my bad english.
I’m very new to php.

The main probles is that i need to put a map name and player count on my server on my html page.

But how to insert php querry code to html?
i tried this:


<?php
require("querry.php");
$server = new SrcSrv("46.174.53.7:27018"); // self explanatory
$info = $server->Info(); // $info is now an array filled with information about the server
echo "<p>Map: </p>".$info["map"] . "        ";
?>

The querry.php file code:


<?php
// srcsrv
// a simple library for querying source engine servers
// by stgn

class BinHelp {
	function __construct($binstring) {
		$this->binstring = $binstring;
		$this->position = 0;
		$this->length = strlen($this->binstring);
	}
	
	function subbin($bytes) {
		if($this->position + $bytes <= $this->length) {
			$sub = substr($this->binstring, $this->position, $bytes);
			$this->position += $bytes;
			return $sub;
		} else { return false; }
	}
	
	function remaining() { return $this->length - $this->position; }
	
	function int8() { if($sub = $this->subbin(1)) return ord($sub); else return false; }	
	function int16() { if($sub = $this->subbin(2)) { $subsub = unpack("v", $sub); return $subsub[1]; } else return false; }
	function int32() { if($sub = $this->subbin(4)) { $subsub = unpack("V", $sub); return $subsub[1]; } else return false; }
	function float32() { if($sub = $this->subbin(4)) { $subsub = unpack("f", $sub); return $subsub[1]; } else return false; }
	function zstring() {
		if($zterm = strpos($this->binstring, "\0", $this->position)) {
			$sub = $this->subbin($zterm - $this->position);
			$this->position++;
			return $sub;
		}
	}
}

class SrcSrv {
	function __construct($server) {
		$this->sock = stream_socket_client("udp://$server");
		stream_set_timeout($this->sock, 2);
		$this->Challenge();
	}
	
	function Challenge() {
		fwrite($this->sock, "\xff\xff\xff\xffU\xFF\xFF\xFF\xFF");
		$resp = fread($this->sock, 1500);
		
		if($resp && $resp[4] == "A") {
			$hb = new BinHelp($resp);
			$hb->subbin(5);
			$this->Challenge = $hb->subbin(4);
			return true;
		}
		return false;
	}
	
	function Info() {
		fwrite($this->sock, "\xff\xff\xff\xffTSource Engine Query\0");
		$resp = fread($this->sock, 1500);
	
		if($resp && $resp[4] == "I") {
			$bh = new BinHelp($resp);
			$bh->subbin(6);
			$retn["name"] = $bh->zstring();
			$retn["map"] = $bh->zstring();
			$retn["gamedir"] = $bh->zstring();
			$retn["desc"] = $bh->zstring();
			$retn["appid"] = $bh->int16();
			$retn["players"] = $bh->int8();
			$retn["max"] = $bh->int8();
			$retn["bots"] = $bh->int8();
			$retn["dedi"] = $bh->subbin(1);
			$retn["os"] = $bh->subbin(1);
			$retn["password"] = (bool)$bh->int8();
			$retn["vac"] = (bool)$bh->int8();
			$retn["version"] = $bh->zstring();

			if($edf = $bh->int8()) {
				if($edf & 0x80)
					$retn["port"] = $bh->int16();
				if($edf & 0x20)
					$retn["tags"] = $bh->zstring();
			}
		
			return $retn;
		}
	
		return false;
	}
	
	function Players() {
		fwrite($this->sock, "\xff\xff\xff\xffU" . $this->Challenge);
		$resp = fread($this->sock, 1500);
		
		if($resp && $resp[4] == "D") {
			$hb = new BinHelp($resp);
			$hb->subbin(6);
			$players = $hb->int8();
			while($hb->remaining() > 0) {
				$hb->int8();
				$retn[] = array("name" => $hb->zstring(), "score" => $hb->int32(), "time" => $hb->float32());
			}
			return $retn;
		}
		
		return false;
	}
}
?>

but it doesn’t seems to work.

Try enabling php error reporting so you can actually get an error.



ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


i got this errors:


Notice: fwrite(): send of 9 bytes failed with errno=1 Operation not permitted in /home/ih267582/public_html/querry.php on line 44

Notice: fwrite(): send of 25 bytes failed with errno=1 Operation not permitted in /home/ih267582/public_html/querry.php on line 57

Did you try adding public access modifiers to your methods?

Seems like a configuration problem, are you using a shared host? Try running a phpinfo() and see what socket methods are disabled/enabled. They’ve probably disabled certain methods in the php configuration.

Just save yourself some time and use this.

Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, sslv2, tls, tlsv1.0, tlsv1.1, tlsv1.2

[editline]22nd December 2016[/editline]

How to use it?

[editline]22nd December 2016[/editline]

When i’m trying to use this, i got error:


Fatal error: Class 'SourceQuery' not found in /home/ih267582/public_html/index.php on line 512

Code that i used:


<?php
			 require("SourceQuery/SourceQuery.php"); // self explanatory
define( 'SQ_SERVER_ADDR', '46.174.53.7:27018' );
	define( 'SQ_SERVER_PORT', 27018 );
	define( 'SQ_TIMEOUT',     1 );
	define( 'SQ_ENGINE',      SourceQuery::SOURCE );
	// Edit this <-
	
	$Query = new SourceQuery( );
	
	try
	{
		$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
		
		print_r( $Query->GetInfo( ) );
		print_r( $Query->GetPlayers( ) );
		print_r( $Query->GetRules( ) );
	}
	catch( Exception $Server )
	{
		"<p>Карта: " . $Server[ 'Map' ] . "</p>";
	}

?>

try “./SourceQuery/SourceQuery.class.php”