-
-
Save sen0rxol0/d4e346c0d505dc8346067262f84cc18d to your computer and use it in GitHub Desktop.
PHP Pinger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// from: http://stackoverflow.com/questions/7372780/creating-a-ping-uptime-service-with-php | |
// TODO: add string recognition to check specific pages | |
//Config information | |
$email = "[email protected]"; | |
$server = "google.com"; //the address to test, without the "http://" | |
$port = "80"; | |
//Create a text file to store the result of the ping for comparison | |
$db = "pingdata.txt"; | |
if (file_exists($db)): | |
$previous_status = file_get_contents($db, true); | |
else: | |
file_put_contents($db, "up"); | |
$previous_status = "up"; | |
endif; | |
//Ping the server and check if it's up | |
$current_status = ping($server, $port, 10); | |
//If it's down, log it and/or email the owner | |
if ($current_status == "down"): | |
echo "Server is down! "; | |
file_put_contents($db, "down"); | |
if ($previous_status == "down"): | |
mail($email, "Server is down", "Your server is down."); | |
echo "Email sent."; | |
endif; | |
else: | |
echo "Server is up! "; | |
file_put_contents($db, "up"); | |
if ($previous_status == "down"): | |
mail($email, "Server is up", "Your server is back up."); | |
echo "Email sent."; | |
endif; | |
endif; | |
function ping($host, $port, $timeout) | |
{ | |
$tB = microtime(true); | |
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout); | |
if (!$fP) { return "down"; } | |
$tA = microtime(true); | |
return round((($tA - $tB) * 1000), 0)." ms"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment