Jump to content

Recommended Posts

Hi guys,

 

I have a function who has to check if a server is online. There is one problem. If the server is not online, I get this error..:

Warning: fsockopen() [function.fsockopen]: unable to connect to 172.31.1.205:80 (Operation timed out) in /Applications/xampp/xamppfiles/htdocs/Periode II/functions/functions.php on line 17

 

I don't want that piece of text.

 

Here is my function, who checkes the availabilty of the server:

// Functions.php     line 14 to 28
// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

 

Here is my piece of code, who uses the function, and who checks the value:

 

// invoeren.php     line 25 to 29
pingDomain($host);
if($status = -1) {
	print "The host is currently not available!";
	return '';
} else {

 

EDIT: What I want is that I check if the server is available, and if not, he has nothing to fill out. If the server is available then the code fills out a form.

 

Thanks!

On a live server, the display_errors setting should be OFF, which would prevent the display of errors.

 

Is there a way in the code itselfs too?? Because the script is not for me alone... And I like it more, if there comes no error...

You can use the output buffer methods (ob_start) or choose to add an @ sign to suppress the errors that may come from fsockopen. The output buffer method is shown below, which is better not sure...

 

function pingDomain($domain){
    $starttime = microtime(true);
    ob_start();
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $message = ob_get_contents();
    ob_end_clean(); // clean the output to avoid the warning being displayed.

    // if operation timed out is found, a timeout was reached, set $file to false
    if (stristr($message, "operation timed out") !== false) 
        $file = false;

    $stoptime  = microtime(true);
    $status    = 0;
   
   if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

 

EDIT:

Fixed the stristr function to pass the right parameters in the correct order.

go with the @ sign hide that error:

 

// Functions.php     line 14 to 28
// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = @fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;
   
   if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

You can use the output buffer methods (ob_start) or choose to add an @ sign to suppress the errors that may come from fsockopen. The output buffer method is shown below, which is better not sure...

 

function pingDomain($domain){
    $starttime = microtime(true);
    ob_start();
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $message = ob_get_contents();
    ob_end_clean(); // clean the output to avoid the warning being displayed.

    // if operation timed out is found, a timeout was reached, set $file to false
    if (stristr($message, "operation timed out") !== false) 
        $file = false;

    $stoptime  = microtime(true);
    $status    = 0;
   
   if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

 

EDIT:

Fixed the stristr function to pass the right parameters in the correct order.

 

Thanks alot! I've used this one instead of the other one. It works, and has a nice timeout..  :P

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.