boemboem Posted January 12, 2009 Share Posted January 12, 2009 Hello all, I've found a script wich can ping and echo a server status, the code is below: <?php // 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; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain"> Domain name: <table> <tr><td><input name="domainname" type="text" ></td></tr> <tr><td><input type="submit" name="submitBtn" value="Ping domain"></td></tr> </table> </form> <?php // Check whether the for was submitted if (isset($_POST['submitBtn'])){ $domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : ''; $domainbase = str_replace("http://","",strtolower($domainbase)); echo '<table>'; $status = pingDomain($domainbase); if ($status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>"; else echo "<tr><td>http://$domainbase is DOWN</td><tr>"; echo '</table>'; } ?> What I want is that I can use this script for give me the outcome of more servers. So I changed the form method to "get" to see the url wich is used. for example this: http://www.mydomain.com/checkwebsitestatus.php?domainname=www.google.nl&submitBtn=Ping+domain When I put this into the adresbar it does not give my a outcome. Can somebody tell me how to fix this script so I can display multiple domains beneath eachothr like this for example * http://www.google.nl is ALIVE (26 ms) * http://www.yahoo.com is ALIVE (133 ms) * http://www.klinkklareonzintoch.nl is DOWN I hope somebody understands what I mean an can help me also. Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/ Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 <?php // 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; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain"> Domain name: <table> <tr> <td> <input name="domainname[]" type="text" > <input name="domainname[]" type="text" > <input name="domainname[]" type="text" > <input name="domainname[]" type="text" > <input name="domainname[]" type="text" > </td> </tr> <tr><td><input type="submit" name="submitBtn" value="Ping domain"></td></tr> </table> </form> <?php // Check whether the for was submitted if (isset($_POST['submitBtn']) && is_array($_POST['domainname'])){ echo '<table>'; foreach($_POST['domainname'] as $domainbase){ if(empty($domainbase)) continue; $domainbase = str_replace("http://","",strtolower($domainbase)); $status = pingDomain($domainbase); if ($status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>"; else echo "<tr><td>http://$domainbase is DOWN</td><tr>"; } echo '</table>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735397 Share on other sites More sharing options...
boemboem Posted January 12, 2009 Author Share Posted January 12, 2009 hello! Thank you for your quick reply! this is allmost what I ment Do you know how to get the output by just fill in the url in the php code, without using a form? Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735466 Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 <?php //Fill this in with the list of servers $servers = array( 'www.google.nl', 'www.yahoo.com', 'www.klinkklareonzintoch.nl', ); // 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; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <body> <table> <?php foreach($_POST['domainname'] as $domainbase){ $status = pingDomain($domainbase); if ($status != -1) echo "<tr><td>$domainbase is ALIVE ($status ms)</td><tr>"; else echo "<tr><td>$domainbase is DOWN</td><tr>"; } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735469 Share on other sites More sharing options...
boemboem Posted January 12, 2009 Author Share Posted January 12, 2009 Great, but ut didn't work yet this is the error message. Warning: Invalid argument supplied for foreach() in /var/www/g35003/coldcharlie.nl/HTML/beta1/multi.php on line 32 this is line 32: foreach($_POST['domainname'] as $domainbase){ Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735479 Share on other sites More sharing options...
DeanWhitehouse Posted January 12, 2009 Share Posted January 12, 2009 Because you are not posting anything Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735483 Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 oops...forgot to change that variable: <?php //Fill this in with the list of servers $servers = array( 'www.google.nl', 'www.yahoo.com', 'www.klinkklareonzintoch.nl', ); // 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; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <body> <table> <?php foreach($servers as $domainbase){ $status = pingDomain($domainbase); if ($status != -1) echo "<tr><td>$domainbase is ALIVE ($status ms)</td><tr>"; else echo "<tr><td>$domainbase is DOWN</td><tr>"; } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735495 Share on other sites More sharing options...
boemboem Posted January 12, 2009 Author Share Posted January 12, 2009 ok, now I have this foreach($servers as $domainbase){ it seems to work, but goves me also this: Is there a way to exclude the warnings?? I think it's part of the fsockopen function, but I really hope it can be fixed, because it looks like s**t on a website line 13: $file = fsockopen ($domain, 80, $errno, $errstr, 10); Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/HTML/beta1/multi.php on line 13 Warning: fsockopen() [function.fsockopen]: unable to connect to www.klinkklareonzintoch.nl:80 (Unknown error) in /var/www/g35003/coldcharlie.nl/HTML/beta1/multi.php on line 13 www.google.nl is ALIVE (27 ms) www.yahoo.com is ALIVE (125 ms) www.klinkklareonzintoch.nl is DOWN [edit]ahh, it was the same idea [/edit] Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735497 Share on other sites More sharing options...
DeanWhitehouse Posted January 12, 2009 Share Posted January 12, 2009 The errors would be because the sites do not exist, but that is what the function should find out so you can suppress the errors by using an @ symbol in front of the fsockopen(); Like so @fsockopen ($domain, 80, $errno, $errstr, 10); Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735503 Share on other sites More sharing options...
rhodesa Posted January 12, 2009 Share Posted January 12, 2009 The errors would be because the sites do not exist, but that is what the function should find out so you can suppress the errors by using an @ symbol in front of the fsockopen(); Like so @fsockopen ($domain, 80, $errno, $errstr, 10); what he said Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735505 Share on other sites More sharing options...
boemboem Posted January 12, 2009 Author Share Posted January 12, 2009 Great!! I really want to thank you both for helping me out. Quote Link to comment https://forums.phpfreaks.com/topic/140524-php-website-alive-status-script-help-needed-please/#findComment-735520 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.