Mastha-Hacker Posted April 14, 2009 Share Posted April 14, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/154091-solved-remove-error-text-or-anti-error-check/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2009 Share Posted April 14, 2009 On a live server, the display_errors setting should be OFF, which would prevent the display of errors. Quote Link to comment https://forums.phpfreaks.com/topic/154091-solved-remove-error-text-or-anti-error-check/#findComment-809993 Share on other sites More sharing options...
Mastha-Hacker Posted April 14, 2009 Author Share Posted April 14, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/154091-solved-remove-error-text-or-anti-error-check/#findComment-809996 Share on other sites More sharing options...
premiso Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154091-solved-remove-error-text-or-anti-error-check/#findComment-809997 Share on other sites More sharing options...
rhodesa Posted April 14, 2009 Share Posted April 14, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/154091-solved-remove-error-text-or-anti-error-check/#findComment-809998 Share on other sites More sharing options...
Mastha-Hacker Posted April 14, 2009 Author Share Posted April 14, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/154091-solved-remove-error-text-or-anti-error-check/#findComment-810003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.