Search code examples
phploopsforeachfsockopen

Online Server Detection with multiple arrays


Heyo, putting together an online/offline detection page for our server cluster. I have managed to get the fsockopen portion working with the array, what I am trying to do now is to link them to second array to echo the specific server and port.

Really new to PHP here so be gentle on me! Been trying to merge the two arrays together without luck. It's obvious I am doing something wrong, just can't figure it out!

$server1 = 'x.x.x.x';
$port1 = [
11000,
11002,
11004,
];
$S1 = [
A1,
A2,
A3,
];

foreach ($port1 as $key => $port1) {
    $fp = @fsockopen($server1,$port1,$errno, $errstr);
    echo "Ping $server1:$port1 $S1 ==> ";
    if ($fp) {
   echo '<p style="color: green; text-align: left">
      "ONLINE"
      </p>';
} else{
   echo '<p style="color: red; text-align: left">
      $S1 OFFLINE
      </p>';

Solution

  • If arrays $port1 and $S1 are in sync with each other, then you can use the key from your loop ($key) to get the value from the second array...

    echo "Ping $server1:$port1 ".$S1[$key]." ==> ";