Code: Select all
is_numeric (var_name)Code: Select all
$ping = "32ms";Moderator: General Moderators
Code: Select all
is_numeric (var_name)Code: Select all
$ping = "32ms";Code: Select all
preg_match('/\d/', $variable)Code: Select all
<div class='width-narrow'>
<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
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";
}
if (isset($domain))
{
$ping = ping("$domain", 80, 10);
}
echo "<div class='site-speed'>
<div class='site-speed-clock'>";
if (!isset($domain)) { echo "<i class='fa fa-clock-o' aria-hidden='true'></i>";}
if (isset($domain) && (1 === preg_match('~[0-9]~', $ping)))
{
if ($ping > 100) { echo "<i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>";}
if ($ping < 100 && $ping > 60) { echo "<i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>";}
if ($ping < 60) { echo "<i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>";}
}
echo "</div>";
if (!isset($domain))
{
echo "Enter your full URL of your website here and click Go.";
}
if (isset($domain) && (1 === preg_match('~[0-9]~', $ping)))
{
//Echoing it will display the ping if the host is up, if not it'll say "down".
echo "The speed Google pings $domain at is: <b>$ping</b>";
if ($ping > 100) { echo "<br/><font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>";}
if ($ping < 100 && $ping > 60) { echo "<br/><font color='#ff5500'>This is quite a good speed.<br/>No real issues, though could be quicker.<br/>Try a F5 to refresh to see if the speed reduces.</font>";}
if ($ping < 60) { echo "<br/><font color='#009900'>This is very good. No issues - very fast!</font>";}
}
if (isset($domain) && (1 !== preg_match('~[0-9]~', $ping)))
{
echo "Sorry the domain you entered is either invalid, or the site you entered is down.";
}
echo "<form method='GET' action='index.php'>
<input type='hidden' name='page' value='website-speed'>
<input type='text' name='domain' ";
if (isset($domain)) { echo "value='$domain'";}
echo "><input type='submit' value='Go'></form></div>";
?><br/>
</div>You mean like a for loop?simonmlewis wrote:Trying to work out how I can make this function do it four times with four separate results in four variables.
How...??Alternately, you could pass the count as a parameter to the ping function and have the for loop inside the function.
Code: Select all
function ping($host, $count = 4, $port = 80, $timeout = 10) {
$ping_times = [];
for ($i = 0; $i < $count; $i++) {
$start_time = microtime(true);
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) {
throw new Exception('Not a valid hostname or domain is down');
}
$end_time = microtime(true);
fclose($fp);
$response_time = round((($end_time - $start_time) * 1000), 0);
$ping_times[] = $response_time;
}
return $ping_times;
}Code: Select all
$pings = ping('domain.com');
$average = array_sum($pings) / count($pings);Code: Select all
function ping($host, $count = 4, $port = 80, $timeout = 10) {
$ping_times = [];
for ($i = 0; $i < $count; $i++) {
$start_time = microtime(true);
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) {
echo "sorry this is not right";
}
$end_time = microtime(true);
fclose($fp);
$response_time = round((($end_time - $start_time) * 1000), 0);
$ping_times[] = $response_time;
}
return $ping_times;
}
if (isset($domain))
{
$ping = ping("www.bbc.co.uk", 80, 10);
$average = array_sum($ping_times) / count($pings);
}
echo "$average";Code: Select all
<?php
function ping($host, $count = 4, $port = 80, $timeout = 10) {
$ping_times = [];
for ($i = 0; $i < $count; $i++) {
$start_time = microtime(true);
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) {
throw new Exception('Not a valid hostname or domain is down');
}
$end_time = microtime(true);
fclose($fp);
$response_time = round(($end_time - $start_time) * 1000);
$ping_times[] = $response_time;
}
return $ping_times;
}
$times = ping('www.bbc.co.uk');
echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";
foreach($times as $time) {
echo $time . "ms<br>";
}