Archive

Posts Tagged ‘check’

Ping A Website Using PEAR’s Net_Ping

April 22nd, 2009 No comments

Net_Ping is a PHP wrapper for the ping program and provides a neat way pinging a host or checking if a site is up and running. To install it you just need to have pear installed. Once that is done you can do a search for PEAR Net_Ping by opening a terminal and typing:

>ping search ping

This will return the following:

Retrieving data...0%.MATCHED PACKAGES, CHANNEL PEAR.PHP.NET:
=======================================
PACKAGE           STABLE/(LATEST) LOCAL
Net_Ping          2.4.4 (stable)        Execute ping
Services_Pingback 0.2.2 (alpha)         A Pingback User-Agent class.

You can install Net_Ping by running the following:

H:>pear install Net_Ping

Which should produce the following output.

downloading Net_Ping-2.4.4.tgz ...
Starting to download Net_Ping-2.4.4.tgz (9,563 bytes)
.....done: 9,563 bytes
install ok: channel://pear.php.net/Net_Ping-2.4.4

You can now use Net_Ping. To use the class in your code you need to include it first, after this you need to get an instance of the Net_Ping object so that you can work with it. The following code will create a Net_Ping object that you can work with.

require_once "Net/Ping.php";
$ping = Net_Ping::factory();

To ping a site you just need to run the ping() function. This function takes a single parameter which is the IP address or the name of the server you want to ping. The function returns a Net_Ping_Result object that contains all of the information returned from the ping.

$results = $ping->ping('www.example.com');

Here is a print_r() output of this object.

Net_Ping_Result Object
(
    [_icmp_sequence] => Array
        (
            [1] => 176
            [2] => 177
        )
 
    [_target_ip] => 208.77.188.166
    [_bytes_per_request] => 32
    [_bytes_total] => 64
    [_ttl] => 54
    [_raw_data] => Array
        (
            [0] => 
            [1] => Pinging www.example.com [208.77.188.166] with 32 bytes of data:
            [2] => 
            [3] => Reply from 208.77.188.166: bytes=32 time=176ms TTL=54
            [4] => Reply from 208.77.188.166: bytes=32 time=177ms TTL=54
            [5] => 
            [6] => Ping statistics for 208.77.188.166:
            [7] =>     Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
            [8] => Approximate round trip times in milli-seconds:
            [9] =>     Minimum = 176ms, Maximum = 177ms, Average = 176ms
        )
 
    [_sysname] => windows
    [_round_trip] => Array
        (
            [min] => 176
            [max] => 177
            [avg] => 176
        )
 
    [_transmitted] => 2
    [_received] => 2
    [_loss] => 0
)

You can access the different properties within the Net_Ping_Result to find out what happened with the ping. The percentage of pings lost is also worked out for you in the _loss property.

echo '<p>Transmitted: '.$results->_transmitted.'</p>';
echo '<p>Received: '.$results->_received.'</p>';
echo '<p>% Loss: '.$results->_loss.'%</p>';

The _round_trip parameter contains an array that details the minimum, maximum and average times for the requests.

You might have noticed in my original example that I only sent out 2 pings. This is because the Net_Ping object has a function that allows you to set certain options before you make the request. This function is called setArgs() and takes an associative array as the argument. To set the number of ping requests to be sent out just use the following line of code before you make your ping request.

$ping->setArgs(array('count' => 2));

You should be aware that different operating systems have different options, so for a full list of options please see the Net_Ping::setArgs() page on the PEAR website.

If you just want to check the host to make sure it is still active you can use the checkhost() function. This works in the same way as the ping() function, but will return true on success and false on failure. You can see the output of checkhost() by viewing the var_dump() output.

var_dump($ping->checkhost('example.com'));

This function is really a wrapper for the ping() function, but will parse the output to determine if the ping request worked or not.

Categories: PHP Tags: , , , , ,

Write PHP Standards Compliant Code With PHP CodeSniffer

October 13th, 2008 1 comment

One of the biggest problems I have found with PHP is the reputation that it gets. This is due to the amount of abuse that the language undergoes by many developers every day. They write code that works, and is efficient, but trying to figure out what it does can take hours as there are no comments, and the code is generally written in any old way. PHP is quite lenient with regards to how code is written.

This is where CodeSnigger comes in. CodeSniffer is a PHP5 script that goes through any PHP (or JavaScript) file that you give it and detects coding violations from a defined set of coding standards. These standards start out with the simple things like using spaces instead of tabs (due to the way in which tabs are displayed differently on different computers) to the more complex things like prefixing private variables with an underscore.

Because CodeSniffer is part of the PEAR framework you can install it using pear commands. As long as PEAR is installed you can use the following command to install CodeSniffer.

pear install PHP_CodeSniffer

If everything has worked you should find a file in your PHP folder called phpcs.bat. You can then use this file to run against your PHP scripts. Here is what to do if you want to run this file against a file.

phpcs class.MyClass.php

This produces something like the following output. I’m not going to reproduce it all as there are quite a few errors.

FILE: class.MyClass.php
--------------------------------------------------------------------------------
FOUND 165 ERROR(S) AND 35 WARNING(S) AFFECTING 123 LINE(S)
--------------------------------------------------------------------------------
1 | ERROR | End of line character is invalid; expected "\n" but found
| | "\r\n"

If you want to check an entire directory then just type in the path to the directory.

phpcs /path/to/file/

This will check each file in turn and produce output.

I thought I was pretty consistent with my coding, but after testing a simple 200 line class I found over 165 errors and 35 warnings for all sorts of things. The main complaint was my use of tabs instead of spaces, for which I can only blame my editor. However, there were a lot of other things such as aligning equals signs with equals signs on other lines.

Using this tool will help you write more understandable code, but will also spot certain syntax errors before they go live. It is good practice to use this tool on your code before you commit your changes as it can prevent you from making silly mistakes like leaving the semi-colon from the end of a line.

Check Or Uncheck All Items In A Checklist With JavaScript

May 6th, 2008 No comments

If you have lots of check boxes in a row a handy little usability trick is to allow a user to click on a button and check all of the checkboxes at once. The following function will either check or uncheck all of the check boxes in your form.

function check_uncheck(truefalse){
 var boxes = document.forms[0].chkboxarray.length;
 var form = document.getElementById('checkForm');
 for(var i=0;i < boxes;i++){
  if(truefalse){
   form.chkboxarray[i].checked=true;
  }else{
   form.chkboxarray[i].checked=false;
  }
 }
}

All you need to do is to add two buttons to your form that will allow users to either check or uncheck the boxes on the form. The function takes a single parameter, which is what to set the value of the check boxes to, so one button is needed to set them to checked and one to unchecked.

<form name="checkForm" id="checkForm">
<input type="checkbox" name="chkboxarray" value="1" /><br />
<input type="checkbox" name="chkboxarray" value="2" /><br />
<input type="button" name="CheckAll" value="Check All Boxes" onclick="check_uncheck(true,3)" />
<input type="button" name="UncheckAll" value="Uncheck All Boxes" onclick="check_uncheck(false,3)" />
</form>

Categories: JavaScript Tags: , , , , ,