Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Lachlan ∆
Lachlan ∆
8,026 Points

What PHP Modules and Requirements Would This Code Have?

Hi, I have a website on a Ubuntu VPS and I'm trying to query two servers for information. However, I didn't write this code and not sure what it requires as it is not working. This code does work when I test it on my local machine however.

<?php
  function query_source($address)
  {
    $array = explode(":", $address);

    $server['status'] = 0;
    $server['ip']     = $array[0];
    $server['port']   = $array[1];

    if (!$server['ip'] || !$server['port']) { exit("EMPTY OR INVALID ADDRESS"); }

    $socket = fsockopen("udp://{$server['ip']}", $server['port'], $errno, $errstr, 1);

    if (!$socket) { return $server; }

    stream_set_timeout($socket, 1);
    stream_set_blocking($socket, TRUE);
    fwrite($socket, "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00");
    $packet = fread($socket, 4096);
    @fclose($socket);

    if (!$packet) { return $server; }

    $header                = substr($packet, 0, 4);
    $response_type         = substr($packet, 4, 1);
    $network_version       = ord(substr($packet, 5, 1));

    if ($response_type != "I") { exit("NOT A SOURCE SERVER"); }

    $packet_array          = explode("\x00", substr($packet, 6), 5);
    $server['name']        = $packet_array[0];
    $server['map']         = $packet_array[1];
    $server['game']        = $packet_array[2];
    $server['description'] = $packet_array[3];
    $packet                = $packet_array[4];
    $app_id                = array_pop(unpack("S", substr($packet, 0, 2)));
    $server['players']     = ord(substr($packet, 2, 1));
    $server['playersmax']  = ord(substr($packet, 3, 1));
    $server['bots']        = ord(substr($packet, 4, 1));
    $server['status']      = 1;
    $server['dedicated']   =     substr($packet, 5, 1);
    $server['os']          =     substr($packet, 6, 1);
    $server['password']    = ord(substr($packet, 7, 1));
    $server['vac']         = ord(substr($packet, 8, 1));

    return $server;
  }


?>

<?php
date_default_timezone_set("Europe/London");
require_once("libraries/TeamSpeak3/TeamSpeak3.php");
TeamSpeak3::init();


$count = 0;
$max = 0;

try {
    $ts3 = TeamSpeak3::factory("serverquery://XXXX:XXXXXXX@XXX.XXX.XXX.XXX:10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1");
    $status = $ts3->getProperty("virtualserver_status");
    $count = $ts3->getProperty("virtualserver_clientsonline") - $ts3->getProperty("virtualserver_queryclientsonline");
    $max = $ts3->getProperty("virtualserver_maxclients");
}
catch (Exception $e) {
    echo "ERROR STUFF I HAD TO REMOVE BECAUSE TREEHOUSE DOESN'T LIKE IT";
}

?>

Thanks, any help would be much appreciated.

3 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

<? Is shorthand declaration for <?php It is bad practice to use it though as it shares the same declaration syntax as XML.

For example:

<?xml version="1.0" encoding="UTF-8" ?>

A lot of web servers have "short_open_tags" disabled by default because of this issue which is probably why the code did not work on your webserver.

Although if you are using PHP 5.4+ you can use the shorthand <?= As this does not clash with XML syntax and has been seperated from "short_open_tags".

Codin - Codesmite
Codin - Codesmite
8,600 Points

Looks like standard PHP to me, do you get any errors when using the code on your web server?

Lachlan ∆
Lachlan ∆
8,026 Points

I was using <? instead of <?php where I declared the code in another file. It's all good now