In this example we use exec() to ping domain names and IP addresses and then parses the results so that you can test the results using an If/Else statement.
exec()
unset()
strpos()
- https://www.php.net/manual/en/function.strpos.php
- https://www.w3schools.com/pHP/func_string_strpos.asp
upDownMonitor.php
<html>
<head>
<meta http-equiv="refresh" content="5">
</head>
<body>
<?php
$websites = array("10.0.1.1","googlecom","salesforce.com","facebook.com", "10.0.1.24");
$good = "1 received";
$successValue;
echo "<h1>Site Status ".date("h:i:s")."</h1>";
foreach ($websites as $url){
unset($result);
$successValue = "DOWN";
exec("ping -c 1 '$url'", $result);
foreach($result as $line) {
if (strpos($line,$good) == TRUE){
$successValue = "UP";
}
}
echo "<strong>Address: ".$url." </strong>";
if ($successValue == "UP") {
echo " Site is ".$successValue;
} else {
echo "Site is ".$successValue;
}
echo "<br><br>";
}
?>
</body>
</html>
Be the first to comment