Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Sprawdzanie czy dany host jest online
Forum PHP.pl > Forum > PHP
TNT
Witam, posiadam stronę internetową oraz serwer gry Soldat. Otóż chciałbym umieścić na swojej stronie skrypt wyświetlający czy mój serwer jest online bądź offline. Aktualnie posiadam taki, ale gdy serwer jest wyłączony, ów skrypt powoduje zmniejszenie szybkości strony do minimum:

  1. <?php
  2. $host = array('blah.selfip.net'); 
  3. $port = array('23073'); 
  4.  
  5. foreach($host as $key => $value) 
  6. { 
  7. $fp = @fsockopen($value, $port[$key]); 
  8. echo 'Server is '; 
  9. if($fp) 
  10. { 
  11. echo '<b><span style="color: green">online</span></b>'; 
  12. } 
  13. else 
  14. { 
  15. echo '<b><span style="color: red">offline</span></b>'; 
  16. } 
  17. echo '<br/>'; 
  18. }
  19. echo '<b><a title="soldat climbing" href="soldat://blah.selfip.net:23073/">Click here to join</a></b>  ';
  20. ?>


Czy mógłby mi ktoś udostępnić skrypt nie obciążający strony gdy serwer jest offline?
devnul
zainteresuj się opcją timeout funkcji fsockopen lub doładowuj tą informacje przy pomocy ajaxa
TNT
Problem w tym, że jestem zielony w PHP a co dopiero AJAXie. Mógłbyś podać konkretne źrodła/linki?
TNT
OK, znalazłem jakiś skrypt. Czy mógłbyś mi podać gdzie wpisać ip?

  1. <?php
  2. // check if a server is up by connecting to a port
  3. function chkServer($host, $port)
  4. {  
  5. $hostip = @gethostbyname($host); // resloves IP from Hostname returns hostname on failure
  6.  
  7. if ($hostip == $host) // if the IP is not resloved
  8. {
  9. echo "Server is down or does not exist";
  10. }
  11. else
  12. {
  13. if (!$x = @fsockopen($hostip, $port, $errno, $errstr, 5)) // attempt to connect
  14. {
  15. echo "Server is down";
  16. }
  17. else 
  18. {
  19. echo "Server is up";
  20. if ($x)
  21. {
  22. @fclose($x); //close connection (i dont know if this is needed or not).
  23. }
  24. }
  25. }
  26. }
  27. ?>


@gethostbyname <- tutaj?
devnul
na dobrą sprawe wystarczy samo to:
  1. <?php
  2. if (!$x = @fsockopen($host, $port, $errno, $errstr, 5)) // attempt to connect
  3. {
  4. echo "Server is down";
  5. }
  6. else 
  7. {
  8. echo "Server is up";
  9. if ($x)
  10. {
  11. @fclose($x); //close connection (i dont know if this is needed or not).
  12. }
  13. }
  14. ?>

$host to np wp.pl lub ip serwer który sprawdzasz - port - to np 80 - domślny port usługi http - pozostałych parametrów nie musisz ruszac
TNT
OK, działa. Wielkie dzięki cool.gif

Jeszcze jedno pytanie:
Gdzie wstawić ip i port w tym skrypcie:

  1. <?php
  2. // PHP ASE parser for Soldat by FliesLikeABrick
  3. // May be used for other games with a few slight modifications
  4. // (mainly removing the part that pretties up the output)
  5. // You are free to use this code for anything you want and redistribute/modify at will
  6.  
  7. function readASEString($sock,$length = false) {
  8. $length = $length ? $length : ord(fread($sock,1));
  9. return fread($sock,$length-1);
  10. }
  11.  
  12. function ASE($host,$port) {
  13. $info = Array();
  14. $s = fsockopen("udp://$host",$port+123);
  15. fwrite($s,"s");
  16. fread($s,4);
  17. readASEString($s);
  18. $info['port'] = readASEString($s);
  19. $info['servername'] = readASEString($s);
  20. $info['ip'] = gethostbyname($host);
  21. $info['mode'] = readASEString($s);
  22. $info['map'] = readASEString($s);
  23. $info['version'] = readASEString($s);
  24. $info['passworded'] = readASEString($s);
  25. $info['numplayers'] = readASEString($s);
  26. $info['maxplayers'] = readASEString($s);
  27.  
  28. // prime the loop conditional
  29. $lengthstring = fread($s,1);
  30. $length = ord($lengthstring);
  31.  
  32. // loop through the key/value pairs and set them
  33.  
  34. while($length != 1) {
  35. $info[readASEString($s,$length)] = readASEString($s);
  36. $lengthstring = fread($s,1);
  37. $length = ord($lengthstring);
  38. }
  39.  
  40. // this entire section can be removed if you don't use them
  41. $info['survival'] = $info['Survival'] == 'No' ? false:true;
  42. $info['realistic'] = $info['Realistic'] == 'No' ? false:true;
  43. $info['remaining'] = explode(' ',$info['Time Left']);
  44. $info['remaining'] = $info['remaining'][0];
  45. $info['minutes'] = explode(':',$info['remaining']);
  46. $info['seconds'] = $info['minutes'][1];
  47. $info['minutes'] = $info['minutes'][0];
  48. $info['timelimit'] = explode(' ',$info['Time Limit']);
  49. $info['timelimit'] = $info['timelimit'][0];
  50. $info['protected'] = $info['Protected'] == 'No' ? false:true;
  51. // end removable section
  52.  
  53. $info['players'] = Array();
  54. for($i=0;$i<$info['numplayers'];$i++) {
  55. $info['players'][$i] = Array();
  56. fread($s,1);
  57. $info['players'][$i]['name'] = readASEString($s);
  58. $info['players'][$i]['team'] = readASEString($s);
  59. $info['players'][$i]['skin'] = readASEString($s);
  60. $info['players'][$i]['score'] = readASEString($s);
  61. $info['players'][$i]['ping'] = readASEString($s);
  62. $info['players'][$i]['time'] = readASEString($s);
  63. }
  64. return $info;
  65. }
  66.  
  67. ?>


O tak?
Oryginał:
$s = fsockopen("udp://$host",$port+123);

Po podstawieniu ip i portu:
$s = fsockopen("udp://blah.selfip.net",23073+123);
devnul
TU:
  1. <?php
  2. function ASE($host,$port) {
  3. ?>

exclamation.gif przeczytałbyś kod który wklejasz to byś wiedział gdzie wstawić host i port a nie zadawał głupie pytania
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.