0

my errors is:

Notice: Undefined variable: return in C:\xampp\htdocs\server4.php on line 21

and the line 21 is:

   $return .= fgets($this->_socket);

Notice: Undefined offset: 0 in C:\xampp\htdocs\server4.php on line 95

Notice: Undefined offset: 0 in C:\xampp\htdocs\server4.php on line 95

and the line 95 is:

$serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']);

and my full code:

<?php
class Q3Master
{
    private $_socket;
    private $_port = '20810';
    private $_host;

    public function __construct($masterHost)
    {
            $this->_socket = fsockopen('udp://79.175.173.73',20810);
            stream_set_blocking($this->_socket,0);
    }

    public function master_listServers($timeout = '1')
    {
            fputs($this->_socket,str_repeat(chr(255),4).'getservers 69 empty full demo'."\n");

            $time=time()+$timeout;
            while($time > time() || strpos($return,'EOT') === FALSE)
            {
                    $return .= fgets($this->_socket);
            }
            $return = explode('\\',$return);
            unset($return[0]);
            unset($return[count($return)]);
            $iplist = array();
            foreach($return as $server)
            {
                    for($i = 0;$i < 4;$i++)
                            $addr[] = ord($server[$i]);

                    for($i = 4;$i < 6;$i++)
                            $port .= dechex(ord($server[$i]));
                    $port = hexdec($port);
                    $iplist[] = array('ip' => join('.',$addr),'port' => $port);
                    unset($addr);
                    unset($port);
            }

            return $iplist;
    }

    function server_getInfo($adresse, $port)
    {
            if($port != 0)
            {
                    $cmd = "\xFF\xFF\xFF\xFFgetstatus";
                    $f = fsockopen('udp://'.$adresse, $port);

                    socket_set_timeout ($f, 1);
                    fwrite ($f, $cmd);
                    $data = fread ($f, 10000);
                    fclose ($f);

     if($data)
     {
        $temp = explode("\x0a",$data);

        $list3 = explode("\\",substr($temp[1],1,strlen($temp[1])));
        for ($i = 0;$i <= count($list3);$i = $i + 2) {
           $list[@$list3[$i]] = @$list3[$i + 1];
        }
        array_pop($list);

        $players = array();
        foreach($temp as $id => $player)
        {
           if($id != 0 AND $id != 1)
           {
              $infos = explode(' ', $player, 3);
              $name = explode('"', $infos[2]);
              $players[] = array('score' => $infos[0], 'ping' => $infos[1], 'name' => $name[1]);
           }
        }
        array_pop($players);

        $infos = array();
        $infos = $list;
        $infos['players'] = $players;

        return $infos;
     }
     else
        return FALSE;
            }
            else
                    return FALSE;
    }
}
$master = new Q3Master('79.175.173.73');

$serverList = $master->master_listServers();


$serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']);

?>
artin es
  • 131
  • 1
  • 3
  • 9

3 Answers3

0

Add an initaliaze of the var return with:

$return = '';

try this:

public function master_listServers($timeout = '1')
    {
            $return = '';
            fputs($this->_socket,str_repeat(chr(255),4).'getservers 69 empty full demo'."\n");

            $time=time()+$timeout;
            while($time > time() || strpos($return,'EOT') === FALSE)
            {
                    $return .= fgets($this->_socket);
            }
            $return = explode('\\',$return);
            unset($return[0]);
            unset($return[count($return)]);
            $iplist = array();
            foreach($return as $server)
            {
                    for($i = 0;$i < 4;$i++)
                            $addr[] = ord($server[$i]);

                    for($i = 4;$i < 6;$i++)
                            $port .= dechex(ord($server[$i]));
                    $port = hexdec($port);
                    $iplist[] = array('ip' => join('.',$addr),'port' => $port);
                    unset($addr);
                    unset($port);
            }

            return $iplist;
    }
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

You need to define $return before using it, because if the while loop will not being executed $return is not defined and therefore cannot being used as a param of explode().

$return = ''; // <-- define it as an empty string

while($time > time() || strpos($return,'EOT') === FALSE)
{
       $return .= fgets($this->_socket);
}
$return = explode('\\',$return);
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

Notice: Undefined variable: return in C:\xampp\htdocs\server4.php on line 21

Then define that variable: $return = null.

Notice: Undefined offset: 0 in C:\xampp\htdocs\server4.php on line 95

Then either define that offset: $serverList[0] = array('ip' => null, 'port' => null), or check whether the offset is defined before using it:

if (isset($serverList[0])) {
  $serverInfo = $master->server_getInfo($serverList[0]['ip'],
                                        $serverList[0]['port']);
}

But first you should investigate why you where expecting $return and $serverList[0] to be defined. Maybe those variables should have been provided by a different part of the code, that is broken.

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • Thanks but know have this problem: `Warning: Missing argument 2 for Q3Master::server_getInfo(), called in C:\xampp\htdocs\server4.php on line 97 and defined in C:\xampp\htdocs\server4.php on line 44 Notice: Undefined variable: port in C:\xampp\htdocs\server4.php on line 46` – artin es Jun 05 '13 at 09:59
  • Which part of "Missing argument 2 for Q3Master::server_getInfo()" don't you understand? As for the undefined `$port` variable: the reson is the same as with `$return`. – Oswald Jun 05 '13 at 10:08