PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 25, 2017 1:48 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 31 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Fri Feb 17, 2017 7:39 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
It should throw one exception, which you'll need to catch. If it's doing something beyond that, post the code you're using and what's happening.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 7:52 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4231
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
<div class='width-narrow'>
<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
if (isset($domain))
{
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;
}

$times = ping('$domain');

echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";

foreach($times as $time) {
    echo $time . "ms<br>";
}
}

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>

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 9:28 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
What's going on here? Why is the function only defined if $domain is set? Why is my example output still in there? Where is $ping being defined? What's with the preg_match?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 9:53 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4231
Location: United Kingdom
This is what I am trying to get to.
So the full page with a bit of info at the top, a form, then info at the bottom.

If I try to ping "hello", it came back with "sock" errors.
So I thought I'd see if I could figure out why.
The preg match is going back to the issue earlier, about numeric values.

Syntax: [ Download ] [ Hide ]
<div class='head'><h1>How fast is your website?</h1></div>
<div class='width-narrow'>
<p>A website's speed is down to server response time, then processing time, and of course images and data time.<br/>
If your website is packed full of images, and your server is slow, this will affect your ranking.<br/>
This simple tool will tell you if your site's server (not the processing of data or images) is good or bad.</p>

<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
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;
}

$times = ping('$domain');

echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";

foreach($times as $ping) {
    echo $ping . "ms<br>";
}

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/>
<h2>How does it find out the speed?</h2>
<p>The tool will 'ping' your website.  It just sends it a little message 'are you there', and it will report back if it is there, and if the time in milliseconds that it took to respond and send back a message.</p>
<p>
If you wish to do this yourself NOT via this tool, just open 'CMD' prompt, and enter 'ping {site domain name}'.
</p>
<p style='text-align: center'><img src='/images/ping.jpg' alt='ping' /></p>
<p>This tool does all that for you via a short system.  It is best to run it 3 or 4 times.  After you have run it.  Just hit F5 and it will run again, and just take an average.  The tool will tell you if it is slow, ok, or really good.</p>
</div>

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 12:06 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
The preg_match is no longer required since you're returning an array of numbers or throwing an exception. Or at least it was. Functions should return values, they shouldn't echo out errors randomly. Finally, try separating out your logic from your display. Things right now look like a huge ball of mud, making it harder to reason about.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 12:45 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
I have rearranged it some and tried to clean up the code.
Syntax: [ Download ] [ Hide ]
<?php

// This section is where we're going to have the domain logic

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("Unable to open connection to '${host}'");
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round((($end_time - $start_time) * 1000), 0);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

$pings = [];
$error_message = '';

// Grab the domain if one has been set
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;

if (isset($domain)) {
    // Try to get ping times. If something goes wrong, we catch the exception
    // and record an error message for display
    try {
        $pings = ping($domain);
    } catch (Exception $e) {
        $error_message =  "Sorry the domain you entered is either invalid, or the site you entered is down.";
    }
}

?>

<?php /* We're going to keep the display logic below */ ?>

<div class="head"><h1>How fast is your website?</h1></div>
<div class="width-narrow">
    <p>
        A website's speed is down to server response time, then processing time, and of course images and data time.<br/>
        If your website is packed full of images, and your server is slow, this will affect your ranking.<br/>
        This simple tool will tell you if your site's server (not the processing of data or images) is good or bad.
    </p>
    <div class="site-speed">
        <div class="site-speed-clock">
            <?php if (!isset($domain)): ?>
                <i class="fa fa-clock-o" aria-hidden="true"></i>
            <?php endif; ?>
        </div>
        <?php if (!isset($domain)): ?>
            Enter your full URL of your website here and click Go.
        <?php endif; ?>

        <?php if (count($pings)): ?>
            The speed Google pings <?php echo $domain; ?> at is:<br>
            <?php foreach ($pings as $ping): ?>
                <b><?php echo $ping; ?>ms</b>
                <?php if ($ping > 100): ?>
                    <i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>
                    <font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>
                <?php elseif ($ping <= 100 && $ping >= 60): ?>
                    <i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>
                    <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>
                <?php elseif ($ping < 60): ?>
                    <i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>
                    <font color='#009900'>This is very good.  No issues - very fast!</font>
                <?php endif; ?>
                <br>
            <?php endforeach; ?>
        <?php elseif (!empty($error_message)): ?>
            <span class="error"><?php echo $error_message; ?></span>
        <?php endif; ?>

        <form method="GET" action="index.php">
            <input type="hidden" name="page" value="website-speed">
            <input type="text" name="domain" value="<?php echo isset($domain) ? $domain : ''; ?>">
            <input type="submit" value="Go">
        </form>
    </div>

    <br/>

    <h2>How does it find out the speed?</h2>
    <p>The tool will 'ping' your website.  It just sends it a little message 'are you there', and it will report back if it is there, and if the time in milliseconds that it took to respond and send back a message.</p>
    <p>If you wish to do this yourself NOT via this tool, just open 'CMD' prompt, and enter 'ping {site domain name}'. </p>
    <p style='text-align: center'><img src='/images/ping.jpg' alt='ping' /></p>
    <p>This tool does all that for you via a short system.  It is best to run it 3 or 4 times.  After you have run it.  Just hit F5 and it will run again, and just take an average.  The tool will tell you if it is slow, ok, or really good.</p>
</div>
 

Also, doing so made me realize that you were earlier asking for four outputs similar to what Windows' ping command would output, but your display appears to be geared toward a single output -- perhaps the average?

More importantly, though, is to not just copy/paste this. Does this make sense to you? Do you understand what I did and why? If not, ask.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 4:01 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4231
Location: United Kingdom
I think so. Not sure I understand the Throw New Exception and the Error Message.
Yes I was asking for four messages, so it's the same as a Ping, but then thought it might be better to just show an average. If I decide to show all four, I can.

Why do you open and close PHP so often in your code?

Syntax: [ Download ] [ Hide ]
<?php elseif ($ping < 60): ?>
                    <i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>
                    <font color='#009900'>This is very good.  No issues - very fast!</font>
                <?php endif; ?>


Would it not be better to do:

Syntax: [ Download ] [ Hide ]
else if ($ping < 60)
{
echo "text here";
}
 


Or maybe it's just your method.
I'll be back on this either tomorrow (Sat) or Monday.
Unless I get bored on Sunday!

Thanks.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2017 4:06 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
simonmlewis wrote:
Why do you open and close PHP so often in your code?

Because I generally keep presentation and business logic separate. I would have ping() in a separate file and have the results passed in. Essentially, I try to keep my presentation files (generally called views) to be mostly HTML with only PHP for simple if/else/loops. Further, I use the alternate syntax so that I don't have to mess with echo and single/double quote juggling. Open an if on one line, go back to normal HTML, then close the if. Keeps it much cleaner and easier to read IMO. That's less important than keeping the logic separated, though, as that promotes code reuse.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 12:04 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4231
Location: United Kingdom
I'm struggling again now.
I see your code echoes the result each time. So it's showing four lots and the good or bad faces.

Syntax: [ Download ] [ Hide ]
 foreach($times as $ping) {
    echo $ping . "ms<br>";
}
                       $averageping = round(array_sum($times) / count($times));

It's this part I am now trying to get in there, to get the average.
So I could say on screen "Your site was pinged four times, these are the results.
Your average is therefore x, which is "good/bad/ugly"....

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 1:47 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
So what is it you're struggling with? Move the good/bad faces out of the foreach loop and use the value of the average ping in those if statements.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 1:51 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
Something like this:
Syntax: [ Download ] [ Hide ]
<?php

// This section is where we're going to have the domain logic

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("Unable to open connection to '${host}'");
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round((($end_time - $start_time) * 1000), 0);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

$pings = [];
$error_message = '';

// Grab the domain if one has been set
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;

if (isset($domain)) {
    // Try to get ping times. If something goes wrong, we catch the exception
    // and record an error message for display
    try {
        $pings = ping($domain);
    } catch (Exception $e) {
        $error_message =  "Sorry the domain you entered is either invalid, or the site you entered is down.";
    }
}

?>

<?php /* We're going to keep the display logic below */ ?>

<div class="head"><h1>How fast is your website?</h1></div>
<div class="width-narrow">
    <p>
        A website's speed is down to server response time, then processing time, and of course images and data time.<br/>
        If your website is packed full of images, and your server is slow, this will affect your ranking.<br/>
        This simple tool will tell you if your site's server (not the processing of data or images) is good or bad.
    </p>
    <div class="site-speed">
        <div class="site-speed-clock">
            <?php if (!isset($domain)): ?>
                <i class="fa fa-clock-o" aria-hidden="true"></i>
            <?php endif; ?>
        </div>
        <?php if (!isset($domain)): ?>
            Enter your full URL of your website here and click Go.
        <?php endif; ?>

        <?php if (count($pings)): ?>
            The speed Google pings <?php echo $domain; ?> at is:<br>
            <?php foreach ($pings as $ping): ?>
                <b><?php echo $ping; ?>ms</b>
                <br>
            <?php endforeach; ?>

            <?php $average_ping = round(array_sum($pings) / count($pings)); ?>
            <?php if ($average_ping > 100): ?>
                <i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>
                <font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>
            <?php elseif ($average_ping <= 100 && $average_ping >= 60): ?>
                <i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>
                <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>
            <?php elseif ($average_ping < 60): ?>
                <i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>
                <font color='#009900'>This is very good.  No issues - very fast!</font>
            <?php endif; ?>
        <?php elseif (!empty($error_message)): ?>
            <span class="error"><?php echo $error_message; ?></span>
        <?php endif; ?>

        <form method="GET" action="index.php">
            <input type="hidden" name="page" value="website-speed">
            <input type="text" name="domain" value="<?php echo isset($domain) ? $domain : ''; ?>">
            <input type="submit" value="Go">
        </form>
    </div>

    <br/>

    <h2>How does it find out the speed?</h2>
    <p>The tool will 'ping' your website.  It just sends it a little message 'are you there', and it will report back if it is there, and if the time in milliseconds that it took to respond and send back a message.</p>
    <p>If you wish to do this yourself NOT via this tool, just open 'CMD' prompt, and enter 'ping {site domain name}'. </p>
    <p style='text-align: center'><img src='/images/ping.jpg' alt='ping' /></p>
    <p>This tool does all that for you via a short system.  It is best to run it 3 or 4 times.  After you have run it.  Just hit F5 and it will run again, and just take an average.  The tool will tell you if it is slow, ok, or really good.</p>
</div>

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 1:55 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4231
Location: United Kingdom
Oh yes I see.
It's a kind of toss up for those who know what this means, and those who don't. But rather than someone having to find the CMD etc... I can point them to the website, which does the same, thing, in a more "attractive" way.

I'll give it a go and let you know what happens.

Thanks a mill.

ps. we are going to be testing PHP 7!!

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 2:00 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
simonmlewis wrote:
ps. we are going to be testing PHP 7!!

Very nice. Keep in mind that mysql_ functions are gone now, so be mindful of which codebases you migrate.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 2:03 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4231
Location: United Kingdom
As in, mysql_query("UPDATE table WHERE id = '$id'@); ??
That won't work in PHP 7 ?
What is the last version of PHP it will work in?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Sun Feb 19, 2017 2:20 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6254
Location: Montreal, Canada
5.6

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 31 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group