未定义的变量和偏移量

时间:2013-06-05 09:41:23

标签: php

我的错误是:

注意:未定义的变量:在第21行的C:\ xampp \ htdocs \ server4.php中返回

,第21行是:

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

注意:未定义的偏移量:第95行的C:\ xampp \ htdocs \ server4.php中的0

注意:未定义的偏移量:第95行的C:\ xampp \ htdocs \ server4.php中的0

并且第95行是:

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

和我的完整代码:

<?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']);

?>

3 个答案:

答案 0 :(得分:0)

使用以下命令添加var return的initaliaze:

$return = '';

试试这个:

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;
    }

答案 1 :(得分:0)

您需要在使用之前定义$return,因为如果while循环未被执行$return未定义,因此无法用作{{1}的参数}。

explode()

答案 2 :(得分:0)

  

注意:未定义的变量:在第21行的C:\ xampp \ htdocs \ server4.php中返回

然后定义该变量:$return = null

  

注意:未定义的偏移量:第95行的C:\ xampp \ htdocs \ server4.php中的0

然后定义该偏移量:$serverList[0] = array('ip' => null, 'port' => null),或者在使用之前检查是否定义了偏移量:

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

但首先,您应该调查为什么要定义$return$serverList[0]。也许这些变量应该由代码的不同部分提供,这是破碎的。