2013-11-10 1 views
1
<?php 
// Edit this -> 
define('MQ_SERVER_ADDR', 'XX.XXX.XXX.XXX'); 
define('MQ_SERVER_PORT', 25565); 
define('MQ_TIMEOUT', 1); 
// Edit this <- 

// Display everything in browser, because some people can't look in logs for errors 
Error_Reporting(E_ALL | E_STRICT); 
Ini_Set('display_errors', true); 

require __DIR__ . '/status/MinecraftQuery.class.php'; 

$Timer = MicroTime(true); 

$Query = new MinecraftQuery(); 

try 
{ 
    $Query->Connect(MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT); 
} 
catch(MinecraftQueryException $e) 
{ 
    $Exception = $e; 
} 

$Timer = Number_Format(MicroTime(true) - $Timer, 4, '.', ''); 
?> 

<link href="css/bootstrap.css" rel="stylesheet" media="screen"> 
<div class="spanx"><p> 
<h1>Login</h1> 
    Username:<br /> 
    <input type="text" name="username" style="height: 30px; style="width: 220px; value="" /> 
    <br/> 
<button>Submit</button> 
<?php // Example from PHP.net 
    $string = '<?php if(($Players = $Query->GetPlayers()) !== false): ?> 
<?php foreach($Players as $Player): ?>'; 
    if(stristr($string, 'Thisshouldbethestringfromthetextbox') === FALSE) { 
    echo 'Player is not online'; 
    } 
?> 

내 코드입니다. 기본적으로 내가하려고하는 것은 내 Minecraft 서버를 쿼리하는 것입니다. 플레이어가 버튼 클릭시 텍스트 양식으로 온라인 상태인지 확인하고 그렇지 않은 경우 플레이어가 온라인 상태가 아니라는 메시지를 전달합니다. 그렇지 않으면 사람이 사이트를 탐색 할 때 로그인 상태를 유지합니다 (이 작업을 수행하는 방법은 알 수 없음). .) 외부 쿼리 파일은 다음과 같습니다버튼을 눌러 PHP 페이지 기능을 호출하려면 어떻게해야합니까?

<?php 
class MinecraftQueryException extends Exception 
{ 
// Exception thrown by MinecraftQuery class 
} 

class MinecraftQuery 
{ 
/* 
* Class written by xPaw 
* 
* Website: http://xpaw.ru 
* GitHub: https://github.com/xPaw/PHP-Minecraft-Query 
*/ 

const STATISTIC = 0x00; 
const HANDSHAKE = 0x09; 

private $Socket; 
private $Players; 
private $Info; 

public function Connect($Ip, $Port = 25565, $Timeout = 3) 
{ 
    if(!is_int($Timeout) || $Timeout < 0) 
    { 
     throw new InvalidArgumentException('Timeout must be an integer.'); 
    } 

    $this->Socket = @FSockOpen('udp://' . $Ip, (int)$Port, $ErrNo, $ErrStr, $Timeout); 

    if($ErrNo || $this->Socket === false) 
    { 
     throw new MinecraftQueryException('Could not create socket: ' . $ErrStr); 
    } 

    Stream_Set_Timeout($this->Socket, $Timeout); 
    Stream_Set_Blocking($this->Socket, true); 

    try 
    { 
     $Challenge = $this->GetChallenge(); 

     $this->GetStatus($Challenge); 
    } 
    // We catch this because we want to close the socket, not very elegant 
    catch(MinecraftQueryException $e) 
    { 
     FClose($this->Socket); 

     throw new MinecraftQueryException($e->getMessage()); 
    } 

    FClose($this->Socket); 
} 

public function GetInfo() 
{ 
    return isset($this->Info) ? $this->Info : false; 
} 

public function GetPlayers() 
{ 
    return isset($this->Players) ? $this->Players : false; 
} 

private function GetChallenge() 
{ 
    $Data = $this->WriteData(self :: HANDSHAKE); 

    if($Data === false) 
    { 
     throw new MinecraftQueryException('Offline'); 
    } 

    return Pack('N', $Data); 
} 

private function GetStatus($Challenge) 
{ 
    $Data = $this->WriteData(self :: STATISTIC, $Challenge . Pack('c*', 0x00, 0x00, 0x00, 0x00)); 

    if(!$Data) 
    { 
     throw new MinecraftQueryException('Failed to receive status.'); 
    } 

    $Last = ''; 
    $Info = Array(); 

    $Data = SubStr($Data, 11); // splitnum + 2 int 
    $Data = Explode("\x00\x00\x01player_\x00\x00", $Data); 

    if(Count($Data) !== 2) 
    { 
     throw new MinecraftQueryException('Failed to parse server\'s response.'); 
    } 

    $Players = SubStr($Data[ 1 ], 0, -2); 
    $Data = Explode("\x00", $Data[ 0 ]); 

    // Array with known keys in order to validate the result 
    // It can happen that server sends custom strings containing bad things (who can know!) 
    $Keys = Array(
     'hostname' => 'HostName', 
     'gametype' => 'GameType', 
     'version' => 'Version', 
     'plugins' => 'Plugins', 
     'map'  => 'Map', 
     'numplayers' => 'Players', 
     'maxplayers' => 'MaxPlayers', 
     'hostport' => 'HostPort', 
     'hostip'  => 'HostIp' 
    ); 

    foreach($Data as $Key => $Value) 
    { 
     if(~$Key & 1) 
     { 
      if(!Array_Key_Exists($Value, $Keys)) 
      { 
       $Last = false; 
       continue; 
      } 

      $Last = $Keys[ $Value ]; 
      $Info[ $Last ] = ''; 
     } 
     else if($Last != false) 
     { 
      $Info[ $Last ] = $Value; 
     } 
    } 

    // Ints 
    $Info[ 'Players' ] = IntVal($Info[ 'Players' ]); 
    $Info[ 'MaxPlayers' ] = IntVal($Info[ 'MaxPlayers' ]); 
    $Info[ 'HostPort' ] = IntVal($Info[ 'HostPort' ]); 

    // Parse "plugins", if any 
    if($Info[ 'Plugins' ]) 
    { 
     $Data = Explode(": ", $Info[ 'Plugins' ], 2); 

     $Info[ 'RawPlugins' ] = $Info[ 'Plugins' ]; 
     $Info[ 'Software' ] = $Data[ 0 ]; 

     if(Count($Data) == 2) 
     { 
      $Info[ 'Plugins' ] = Explode("; ", $Data[ 1 ]); 
     } 
    } 
    else 
    { 
     $Info[ 'Software' ] = 'Vanilla'; 
    } 

    $this->Info = $Info; 

    if($Players) 
    { 
     $this->Players = Explode("\x00", $Players); 
    } 
} 

private function WriteData($Command, $Append = "") 
{ 
    $Command = Pack('c*', 0xFE, 0xFD, $Command, 0x01, 0x02, 0x03, 0x04) . $Append; 
    $Length = StrLen($Command); 

    if($Length !== FWrite($this->Socket, $Command, $Length)) 
    { 
     throw new MinecraftQueryException("Failed to write on socket."); 
    } 

    $Data = FRead($this->Socket, 2048); 

    if($Data === false) 
    { 
     throw new MinecraftQueryException("Failed to read from socket."); 
    } 

    if(StrLen($Data) < 5 || $Data[ 0 ] != $Command[ 2 ]) 
    { 
     return false; 
    } 

    return SubStr($Data, 5); 
} 
} 

나는 내가 할 수있는 어떤 식 으로든이 문제를 해결하고 싶습니다. 미리 감사 드리며 필요한 모든 질문을하십시오. D.

답변

0

동일한 URL을 가리키는 양식을 만들어이를 수행 할 수 있습니다. 양식이 전송 될 때 쿼리 데이터의 문자열이 제출 한 내용과 일치하는 경우

<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post"> 
    Username: <input type="text" name="username" /> 
    <button type="submit">Send</button> 
</form> 

, 당신은 어떻게 당신이 테스트에 액세스 할 $_POST['username']

if (isset($_POST['username'])) { 
    // your code here 
    echo 'Username: ' . $_POST['username']; 
} 
+0

에 액세스하여 입력 콘텐츠에 액세스 할 수 있습니까? 또한, 내 PHP는 분명히 오류가 있습니다. – Minedude78910

+0

그래, 왜 PHP 코드를 문자열 안에 넣었 니? ($ string = ' GetPlayers())! == false) :?>') –

+0

잘 모르겠습니다. 로터리없이이 문제를 해결할 수있는 완벽한 방법이 있습니까? – Minedude78910

관련 문제