Hallo
ich weiß nicht recht wie ich meinen fall beschrieben soll
Ich habe im internet ein script gefunden das per klick eine php seite läd/ausführt und mir einen response zurückgeben soll. Funktioniert soweit auch ganz gut, aber wenn ich das alles dann mit meinem script verbinden will passiert einfach nichts mehr.
Es steht auf der seite einfach immer nur das "processing..."
Ich hoffe jemand findet etwas im code und kann mir erklären warum das nicht funktioniert. Einzeln funktioniert ja alles
BTW: ich weiß das die IDs noch unschön sind. Dennoch sollte es ja funktionieren.
index.php
xhr.php
q3query.class.php
ich weiß nicht recht wie ich meinen fall beschrieben soll
Ich habe im internet ein script gefunden das per klick eine php seite läd/ausführt und mir einen response zurückgeben soll. Funktioniert soweit auch ganz gut, aber wenn ich das alles dann mit meinem script verbinden will passiert einfach nichts mehr.
Es steht auf der seite einfach immer nur das "processing..."
Ich hoffe jemand findet etwas im code und kann mir erklären warum das nicht funktioniert. Einzeln funktioniert ja alles
Code:
document.getElementById("status").innerHTML = "processing...";
BTW: ich weiß das die IDs noch unschön sind. Dennoch sollte es ja funktionieren.
index.php
Code:
<form name="testForm" action="" method=""> code: <input type="text" name="fname" id="fname" /><br /> <input type="button" value="Submit Form" onclick="postStuff();" /> </form> <div id="status"></div> <script> function postStuff(){ // Create our XMLHttpRequest object var hr = new XMLHttpRequest(); // Create some variables we need to send to our PHP file var url = "xhr.php"; var fn = document.getElementById("fname").value; var vars = "firstname="+fn; hr.open("POST", url, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // Access the onreadystatechange event for the XMLHttpRequest object hr.onreadystatechange = function() { console.log(hr); if(hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("status").innerHTML = return_data; } } // Send the data to PHP now... and wait for response to update the status div hr.send(vars); // Actually execute the request document.getElementById("status").innerHTML = "processing..."; } </script>
Code:
<?php $command = $_POST['firstname']; require("q3query.class.php"); $con = new q3query("secretip", secretport, $success); if (!$success) { die ("Fehler bei der Verbindungherstellung"); } $con->setRconpassword("secretrconpassword"); echo $con->rcon("$command"); } ?>
Code:
<?php /* q3query.class.php - Quake 3 query class */ class q3query { private $address; private $port; private $rconpassword = false; private $fp; private $lastPing = false; public function __construct($address, $port, &$success = NULL, &$errno = NULL, &$errstr = NULL) { $this->address = $address; $this->port = $port; $this->fp = fsockopen("udp://$address", $port, $errno, $errstr, 5); if (!$this->fp) { $success = false; } else { $success = true; } } public function setRconpassword($pw) { $this->rconpassword = $pw; } public function rcon($str) { if (!$this->rconpassword) { return false; } $this->send("rcon " . $this->rconpassword . " $str"); return $this->getResponse(); } private function send($str) { fwrite($this->fp, "\xFF\xFF\xFF\xFF$str\x00"); } private function getResponse() { stream_set_timeout($this->fp, 0, 7e5); $s = ''; $start = microtime(true); do { $read = fread($this->fp, 9999); $s .= substr($read, strpos($read, "\n") + 1); if (!isset($end)) { $end = microtime(true); } $info = stream_get_meta_data($this->fp); } while (!$info["timed_out"]); $this->lastPing = round(($end - $start) * 1000); return $s; } public function quit() { if (is_resource($this->fp)) { fclose($this->fp); return true; } return false; } public function reconnect() { $this->quit(); $this->__construct($this->address, $this->port); } public function getGameStatus() { $this->send("getstatus"); $response = $this->getResponse(); list($dvarslist, $playerlist) = explode("\n", $response, 2); $dvarslist = explode("\\", $dvarslist); $dvars = array(); for ($i = 1; $i < count($dvarslist); $i += 2) { $dvars[$dvarslist[$i]] = $dvarslist[$i + 1]; } $playerlist = explode("\n", $playerlist); array_pop($playerlist); $players = array(); foreach ($playerlist as $value) { list($score, $ping, $name) = explode(" ", $value, 3); $players[] = array( "name" =>substr($name, 1, -1), "score" => $score, "ping" => $ping ); } return array($dvars, $players); } public function getGameInfo() { $this->send("getinfo"); $response = $this->getResponse(); $dvarslist = explode("\\", $response); $dvars = array(); for ($i = 1; $i < count($dvarslist); $i += 2) { $dvars[$dvarslist[$i]] = $dvarslist[$i + 1]; } return $dvars; } public function getLastPing() { return $this->lastPing; } } ?>
Kommentar