Scripts

phPing V1.4

Met phPing V1.4 kun je een host (web-adres of IP-adres) "pingen" om te zien of de website reageert. Het script is in een class gestopt, hoewel de functie eigenlijk maar uit 1 functie bestaat welke uiteindelijk alles doet. Voorbeeld: Wat als resultaat geeft: phPing V1.4 Host: phphulp.nl:80 Count: 4 Results: Array ( => 0.0337s => 0.0321s => 0.0354s => 0.0334s ) Je kan tevens de poort aanpassen, en hoeveel keer je de host wilt pingen. Zoals goed is opgemerkt, is dit inderdaad niet een soortgelijke PING als die van jouw computer. Mocht er iemand tijd en zin in hebben, maak er een ;-)

phping.php
<?php
/*
 * phPing - PHP Ping Tool
 * Public version 1.4 
 * Released on 08-09-10
 * 
 * Free to share, keep the above please :-)
 */

class phPing {

	public $result;
	public $host;
	public $port;
	public $count;
	public $timeout;
	public $errornumber;
	public $errorstring;
	
	public function __construct($host, $port, $count, $timeout = 5)
	{
		$this -> host = $host;
		$this -> port = $port;
		$this -> count = $count;
		$this -> timeout = $timeout;	
		
		return $this -> ping();
	}
	
	final protected function ping()
	{
		for($p = 1; $p <= $this -> count; $p++)
		{
			$time = explode(' ', microtime());
			$start = $time[1] + $time[0];
			
			$tmp_socket = @fsockopen($this -> host, $this -> port, $this -> errornumber, $this -> errorstring, $this -> timeout);
			
			$time = explode(' ', microtime());
			$end = $time[1] + $time[0];
			
			if(empty($this -> errorstring))
			{
				$this -> result[$p] = round(($end - $start), 4).'s';
			}
			else
			{
				$this -> result[$p] = $this -> errorstring;	
			}
		}
		
		return $this -> result;
	}
}

$object = new phPing('phphulp.nl', 80, 4);
echo 'phPing V1.4<br />';
echo 'Host: '.$object -> host.':'.$object -> port.'<br />';
echo 'Packets: '.$object -> count.'<br />';
echo 'Results:<pre>'.print_r($object -> result, true).'</pre>';

Reacties

0
Nog geen reacties.