I have a script that performs an fsockopen command in a while loop. Basically it checks several ports on one server.
The problem is that if it fails to open a port because it is down or non-existant then it completely fails when it moves the next port. I guess this is because the previous fsockopen call is still trying to connect to the port or something similar.
Does anyone know of any way to kill an fsockopen command if it fails. I can't use die() or the equivalent as I need to perform some database updates if the fsockopen call fails.
Code is as follows
while ($sql_server_ports_result = mysql_fetch_array($sql_server_ports_query))
{
$fp = @fsockopen ($sql_server_result["server_address"], $sql_server_ports_result["Port_Number"], $errno, $errstr, 20);
// if we can't get to the port then it is down
if (!$fp)
{
print $sql_server_ports_result["port_name"]." [".$sql_server_ports_result["Port_Number"]."] <img src=\"graphics/red.gif\" align=\"middle\">";
// create the log entry
$sql_create_log_query = mysql_query("INSERT INTO az_logs SET server_ports_ID = ".$sql_server_ports_result["Port_Number"].", creation_date=NOW(), servers_ID = ".$sql_server_result["ID"].", status='down'",$db) or die("Error creating log: " .mysql_error());
// if the server responds and is up
} else {
print $sql_server_ports_result["port_name"]." [".$sql_server_ports_result["Port_Number"]."] <img src=\"graphics/green.gif\" align=\"middle\">";
fclose ($fp);
} // end of if that checks if server is up
print "   ";
} // end of while loop that goes through the server ports