Page 1 of 1

php: how to communicate with quake2 server?

Posted: Fri Apr 23, 2004 3:46 am
by spectre
yeah, simple example, hope ppl can use this as frame:

(there was a bug in php that did prevent reading from udp stream, tho it was fixed in 4.3.1 (or smthng), so you'll need recent version of php for this.


Code: Select all


<?php

//set servers rcon password

$rcon_password = "SecreT"



//command to send to server

$yourcommand = "say message from outer world: it's SUMMER!";



//open connection

$conn = fsockopen( "udp://10.0.0.1", 27910, $error, $errdescription, 10 );



//check if connection was made

if ($conn) {



   //send data to server

   fputs( $conn, "\xff\xff\xff\xff".$yourcommand);



   //parse output

   $output = str_replace("ÿ", "", (fread( $conn, 1024 )));



   //aaand make array out of it

   $reply = split ("\x0a", $output);



   //then let's see what we wanna do with those rows that server sent back

   foreach ($reply as $r) {



     //as i am simple man, i don't wanna do anything but print the output

     print $r."\n";

   }

}

//something ain't working

else {

 

   //i've seen it all

   die("ERROR with connection: ".$errdescription);



}

?>


Posted: Fri Apr 23, 2004 6:04 am
by Den
Diddnt the last q2 update (3.21?) block msges like that?



Oh w8 ur using rcon :) nevermind

Posted: Fri Apr 23, 2004 6:24 am
by Da^JuaN
where can you use it for especially?

Posted: Fri Apr 23, 2004 8:00 am
by Den
for loadsa things



like set a server with eaql settings and map changes :)



quite easy to do

Posted: Fri Apr 23, 2004 11:59 am
by spectre
yeah, just add fputs-lines for every rcon command you want to issue, and so on. you don't NEED to check the reply btw. if you just wanna execute rcon commands, skip the postprocessing.



and simple remote console for server would be:


Code: Select all


<html>

<body>

<?php

//set server's rcon password

$rcon_password = $_POST['rcon'];



//command to send to server

$yourcommand = $_POST['command'];



//server address

$serveraddr = $_POST['server'];



//open connection

$conn = fsockopen($serveraddr, 27910, $error, $errdescription, 10);



//check if connection was made

if ($conn) {



   //send data to server

   fputs( $conn, "\xff\xff\xff\xff".$yourcommand);



   //parse output

   $output = str_replace("ÿ", "", (fread( $conn, 1024 )));



   //aaand make array out of it

   $reply = split ("\x0a", $output);



   //then let's see what we wanna do with those rows that server sent back

   foreach ($reply as $r) {



     //as i am simple man, i don't wanna do anything but print the output

     print $r."<br>\n";

   }

}

print "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";

?>

<input type="text" name="server" size="20"> : server address<br>

<input type="text" name="rcon" size="20"> : rcon password<br>

<input type="text" name="command" size="20"> : command<br>

<input type="submit">

</form>

</body>

</html>


Posted: Fri Apr 23, 2004 12:21 pm
by Da^JuaN
me php n00b dont get this at all, maybe if i understand it sometime.. i will look at it ;)

Posted: Sat Apr 24, 2004 2:02 am
by Rallu
spectre, this looks intresting... I have used a bit these php -> q2 things, but I haven't put enough effort to resolve those things what you need to send to q2 server...





where did you hack that '\xff\xff\xff\xff'? from q2 source?

Posted: Sat Apr 24, 2004 4:53 am
by kaaos
i copypasted everything from that finished script but for some reason it doesn't work, it just prints:



Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/kaaos/public_html/q2server.php on line 14



Warning: fsockopen(): unable to connect to :27910 in /home/kaaos/public_html/q2server.php on line 14




and the input boxes below it.



:scratch:

Posted: Sat Apr 24, 2004 11:52 am
by spectre
Rallu wrote:where did you hack that '\xff\xff\xff\xff'? from q2 source?


nope, just captured the rcon packets from homenetwork, simple logic helped me a bit too ;)



kaaos:

seems like your php can't handle udp connections, what's the version and os the php runs on? shouldn't be os relevant, but anyway.