LetMeLearn123
LetMeLearn123

Reputation: 61

Checking server status - online/offline

I have a loop showing info of many different servers from database. Now i am trying to write if they are online or offline. I have tried to write manually the ip address and port to the srcipt and it works fine. However when i tried to put the table into the variable it shows that all are offline. Most of them use 44405 port so i put it manually for now to check it.

EDIT: yes i know that this mysql code is ready and waiting for injection :D

<?php //-----loading servers to website-----
error_reporting(E_ALL); 
ini_set('display_errors','1');

$connection = //deleted this part coz includes personal info :D


$alldata = mysql_query("SELECT * FROM muonline ORDER BY id DESC;");
while ($loadservers = mysql_fetch_assoc($alldata)) {


echo "<tr>";

  echo '<td>';  //-----here starts the online/offline part of script-----

    $serwer = $loadservers['serverAddress'];
    $port="44405";
    $socket=@fsockopen($serwer,$port,$errno,$errstr,2);

    if($socket==true)
    {
     echo "<p>online</p>";
    }else{
     echo "<p>offline</p>";
    }
    echo '</td>';

//------- here is being loaded rest of info of the each server-----
echo "<td><p>" .$loadservers['serverName']."</p></td>";
echo "<td><p>". '<a href="' .$loadservers['serverAddress'].'"target="_blank"><p>'.$loadservers['serverAddress']. "</p></a>". "</td>";
echo "<td><p>" .$loadservers['serverExp']. "</p></td>";
echo "<td><p>" .$loadservers['serverDrop']. "</p></td>";
echo "<td><p>" .$loadservers['info']. "</p></td>";
echo "</tr>";
}

mysql_close();
?>

Upvotes: 1

Views: 3281

Answers (1)

Dan Smith
Dan Smith

Reputation: 5685

You're attempting to use fsockopen with a server starting with http://. You'll need to parse the URL using parse_url, extract the host part of the URL and check using that. Like this:

$parsedUrl = parse_url($loadservers['serverAddress']);
$socket = fsockopen($parsedUrl['host'],$port,$errno,$errstr,2);

Upvotes: 3

Related Questions