Jump to content

PHP help

Go to solution Solved by Neil,

okay now i have added this but still blank?

-snip-

Nope. You don't need to set $host. You need to call the function with a variable. Like this:

echo ping("8.8.8.8");
Put that line outside of the function. It should look like this:

2015-05-12_1604.png

so im building a server back end and i have this code i made

 

<?php
$web = "www.google.com";
$result = system("ping -c 1 $web" );
echo "Your ping to our network is $result ms";

?>

and on screen this is what i get 

Pinging www.google.com [216.58.210.68] with 32 bytes of data: Reply from 216.58.210.68: bytes=32 time=25ms TTL=52 Reply from 216.58.210.68: bytes=32 time=25ms TTL=52 Reply from 216.58.210.68: bytes=32 time=27ms TTL=52 Reply from 216.58.210.68: bytes=32 time=28ms TTL=52 Ping statistics for 216.58.210.68: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 25ms, Maximum = 28ms, Average = 26ms Your ping to our network is Minimum = 25ms, Maximum = 28ms, Average = 26ms ms

 

at the current moment im only interested in the average ping in ms. in php how could i get it so i can have the avrg ping saved in a variable? 

thanks in advance

~James

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/
Share on other sites

Link to post
Share on other sites

You could try this (Stack Overflow - Ping an ip using php and result in only the average response time.)

<?php    function GetPing($ip=NULL) {     if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];}     if(getenv("OS")=="Windows_NT") {       $ping=explode(",", $exec);       return $ping[1];//Maximum = 78ms     }     else {      $exec = exec("ping -c 3 -s 64 -t 64 ".$ip);      $array = explode("/", end(explode("=", $exec )) );      return ceil($array[1]) . 'ms';     }    }    echo GetPing();?>
Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949041
Share on other sites

Link to post
Share on other sites

 

You could try this (Stack Overflow - Ping an ip using php and result in only the average response time.)

<?php    function GetPing($ip=NULL) {     if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];}     if(getenv("OS")=="Windows_NT") {       $ping=explode(",", $exec);       return $ping[1];//Maximum = 78ms     }     else {      $exec = exec("ping -c 3 -s 64 -t 64 ".$ip);      $array = explode("/", end(explode("=", $exec )) );      return ceil($array[1]) . 'ms';     }    }    echo GetPing();?>

chwwea, im gonna sound like such an idiot but where does the web adress go?

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949123
Share on other sites

Link to post
Share on other sites

First things first, using system/exec is a huge security hole unless you properly sanitize your input. That means making sure that what you're getting is a proper hostname and nothing more. If you're not sure what you're doing, you probably shouldn't be doing it in production.

 

system() doesn't really return any output, it just dumps it out. So unless you want to mess around with catching that output, it's easier to use exec().

 

This code is a bit longer because it does all the checks you might want, but it won't run on Windows. Your output looks like you're running it locally on Windows, but if you plan on putting it on a server somewhere, it will probably be running on Linux. If you really need it to run on Windows, it shouldn't be that hard to modify it. I'm not on Windows so I can't do that right now.

<?phpfunction ping($web)	{	$matches = array();	preg_match('/^(([0-9]+\.){3}([0-9]+))$/', $web, $matches);	if(count($matches) > 0 && $matches[0] == $web) $host = $web;	else		{		$hostByName = gethostbyname($web);		if($hostByName != $web) $host = $hostByName;		}	if(isset($host))		{		$execResult = array();		exec('ping -c 2 "' . $host . '" | tail -n 1', $execResult);				if(count($execResult) == 0) return false;				$lastRow = explode('/', $execResult[count($execResult) - 1]);				if(count($lastRow) == 7) return $lastRow[4];		}		return false;	}$web = 'www.google.com';var_dump(ping($web));$web = '127.0.0.1';var_dump(ping($web));$web = 'bad-domain';var_dump(ping($web));$web = 'dangerous input; echo "get hacked"';var_dump(ping($web));?>
Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949135
Share on other sites

Link to post
Share on other sites

 

First things first, using system/exec is a huge security hole unless you properly sanitize your input. That means making sure that what you're getting is a proper hostname and nothing more. If you're not sure what you're doing, you probably shouldn't be doing it in production.

 

system() doesn't really return any output, it just dumps it out. So unless you want to mess around with catching that output, it's easier to use exec().

 

This code is a bit longer because it does all the checks you might want, but it won't run on Windows. Your output looks like you're running it locally on Windows, but if you plan on putting it on a server somewhere, it will probably be running on Linux. If you really need it to run on Windows, it shouldn't be that hard to modify it. I'm not on Windows so I can't do that right now.

<?phpfunction ping($web)	{	$matches = array();	preg_match('/^(([0-9]+\.){3}([0-9]+))$/', $web, $matches);	if(count($matches) > 0 && $matches[0] == $web) $host = $web;	else		{		$hostByName = gethostbyname($web);		if($hostByName != $web) $host = $hostByName;		}	if(isset($host))		{		$execResult = array();		exec('ping -c 2 "' . $host . '" | tail -n 1', $execResult);				if(count($execResult) == 0) return false;				$lastRow = explode('/', $execResult[count($execResult) - 1]);				if(count($lastRow) == 7) return $lastRow[4];		}		return false;	}$web = 'www.google.com';var_dump(ping($web));$web = '127.0.0.1';var_dump(ping($web));$web = 'bad-domain';var_dump(ping($web));$web = 'dangerous input; echo "get hacked"';var_dump(ping($web));?>

yeah im new to php thank you, im on windows this is what i get 

boolean false

boolean false

boolean false

boolean false

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949150
Share on other sites

Link to post
Share on other sites

chwwea, im gonna sound like such an idiot but where does the web adress go?

 

At a glance I would assume it goes between the ( & ). - The IP address is an optional param. Not given, the script pings you by default.

For example;

 

echo GetPing();

//Will ping you by your external IP Address

or

echo GetPing("8.8.8.8");

//Will ping the given ip address

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949194
Share on other sites

Link to post
Share on other sites

At a glance I would assume it goes between the ( & ). - The IP address is an optional param. Not given, the script pings you by default.

For example;

 

echo GetPing();

//Will ping you by your external IP Address

or

echo GetPing("8.8.8.8");

//Will ping the given ip address

hmm slowly getting there!!! 2 errors :( undefined variable exec line 5 and undefined offset line 6. learning php for 3 weeks now so not great at it

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949220
Share on other sites

Link to post
Share on other sites

yeah im new to php thank you, im on windows this is what i get 

boolean false

boolean false

boolean false

boolean false

 

Windows ping gives different output from Linux's. I don't have access to a windows box, so this code wasn't tested on Windows and will probably need some modification of the //windows block.

<?phpfunction ping($web)	{	$matches = array();	preg_match('/^(([0-9]+\.){3}([0-9]+))$/', $web, $matches);	if(count($matches) > 0 && $matches[0] == $web) $host = $web;	else		{		$hostByName = gethostbyname($web);		if($hostByName != $web) $host = $hostByName;		}	if(isset($host))		{		if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')			{			// windows			$execResult = array();			exec('ping "' . $host . '"', $execResult);						if(count($execResult) == 0) return false;						$lastRow = explode('Average = ', $execResult[count($execResult) - 1]);						if(count($lastRow) == 2) return $lastRow[1];			}		else			{			// *nix			$execResult = array();			exec('ping -c 2 "' . $host . '"', $execResult);						if(count($execResult) == 0) return false;						$lastRow = explode('/', $execResult[count($execResult) - 1]);						if(count($lastRow) == 7) return $lastRow[4];			}		}		return false;	}$web = 'www.google.com';var_dump(ping($web));$web = '127.0.0.1';var_dump(ping($web));$web = 'bad-domain';var_dump(ping($web));$web = 'dangerous input; echo "get hacked"';var_dump(ping($web));?>
Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949313
Share on other sites

Link to post
Share on other sites

 

Windows ping gives different output from Linux's. I don't have access to a windows box, so this code wasn't tested on Windows and will probably need some modification of the //windows block.

<?phpfunction ping($web)	{	$matches = array();	preg_match('/^(([0-9]+\.){3}([0-9]+))$/', $web, $matches);	if(count($matches) > 0 && $matches[0] == $web) $host = $web;	else		{		$hostByName = gethostbyname($web);		if($hostByName != $web) $host = $hostByName;		}	if(isset($host))		{		if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')			{			// windows			$execResult = array();			exec('ping "' . $host . '"', $execResult);						if(count($execResult) == 0) return false;						$lastRow = explode('Average = ', $execResult[count($execResult) - 1]);						if(count($lastRow) == 2) return $lastRow[1];			}		else			{			// *nix			$execResult = array();			exec('ping -c 2 "' . $host . '"', $execResult);						if(count($execResult) == 0) return false;						$lastRow = explode('/', $execResult[count($execResult) - 1]);						if(count($lastRow) == 7) return $lastRow[4];			}		}		return false;	}$web = 'www.google.com';var_dump(ping($web));$web = '127.0.0.1';var_dump(ping($web));$web = 'bad-domain';var_dump(ping($web));$web = 'dangerous input; echo "get hacked"';var_dump(ping($web));?>

i wopulent know how to do that i have only been doing php for 3 weeks! and this is a little side project for a website me and someone else are making, so we can monitor its status along with other of mine and his sites

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949330
Share on other sites

Link to post
Share on other sites

Yeah yeah yeah.. Uh.. Lets go ahead and never use exec... It's a bad idea. Like. A very bad idea.. in almost all situations.

 

You're much better off doing the following. It sends a ICMP ping packet with a pre-calculated checksum.

 

 

function ping($host){    $payload = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";    $socket = socket_create(AF_INET, SOCK_RAW, 1);    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));    socket_connect($socket, $host, null);    $ts = microtime(true);    socket_send($socket, $payload, strlen($payload), 0);    if (socket_read($socket, 255))    {        $result = microtime(true) - $ts;    } else     {        $result = false;    }    socket_close($socket);    return $result;}

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949331
Share on other sites

Link to post
Share on other sites

Yeah yeah yeah.. Uh.. Lets go ahead and never use exec... It's a bad idea. Like. A very bad idea.. in almost all situations.

 

You're much better off doing the following. It sends a ICMP ping packet with a pre-calculated checksum.

 

 

function ping($host){    $payload = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";    $socket = socket_create(AF_INET, SOCK_RAW, 1);    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));    socket_connect($socket, $host, null);    $ts = microtime(true);    socket_send($socket, $payload, strlen($payload), 0);    if (socket_read($socket, 255))    {        $result = microtime(true) - $ts;    } else     {        $result = false;    }    socket_close($socket);    return $result;}

just ran it nothing pops up on screen which is always good, and i also dont get where i enter the adress

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949364
Share on other sites

Link to post
Share on other sites

just ran it nothing pops up on screen which is always good, and i also dont get where i enter the adress

So functions like this take what are called parameters 2015-05-12_1543.png

That means you supply it with a value, and it uses that value inside itself.

In addition, this function doesn't print out anything--it returns a value, so you have to create a variable to hold the return value of the function, like this:

 

$time = ping("8.8.8.8");
Example output (with echo):

2015-05-12_1554.png

Then you can do whatever you want with $time. Print it, manipulate it, whatever.

Quick correction to that function...

 

function ping($host){	$payload = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";	$socket = socket_create(AF_INET, SOCK_RAW, 1);	socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 300, 'usec' => 0));	socket_connect($socket, $host, null);	$ts = microtime(true);	socket_send($socket, $payload, strlen($payload), 0);	if (socket_read($socket, 255)) {		$result = microtime(true) - $ts;	} else {		$result = false;	}	socket_close($socket);	return $result;}

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949512
Share on other sites

Link to post
Share on other sites

So functions like this take what are called parameters 2015-05-12_1543.png

That means you supply it with a value, and it uses that value inside itself.

In addition, this function doesn't print out anything--it returns a value, so you have to create a variable to hold the return value of the function, like this:

 

$time = ping("8.8.8.8");
Example output (with echo):

2015-05-12_1554.png

Then you can do whatever you want with $time. Print it, manipulate it, whatever.

Quick correction to that function...

 

function ping($host){	$payload = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";	$socket = socket_create(AF_INET, SOCK_RAW, 1);	socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 300, 'usec' => 0));	socket_connect($socket, $host, null);	$ts = microtime(true);	socket_send($socket, $payload, strlen($payload), 0);	if (socket_read($socket, 255)) {		$result = microtime(true) - $ts;	} else {		$result = false;	}	socket_close($socket);	return $result;}

okay now i have added this but still blank?

<?php

$host = "www.google.com";

function ping($host)

{

$payload = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";

$socket = socket_create(AF_INET, SOCK_RAW, 1);

socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 300, 'usec' => 0));

socket_connect($socket, $host, null);

$ts = microtime(true);

socket_send($socket, $payload, strlen($payload), 0);

if (socket_read($socket, 255)) {

  $result = microtime(true) - $ts;

} else {

  $result = false;

}

socket_close($socket);

return $result;

echo'$result';

}

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949557
Share on other sites

Link to post
Share on other sites

okay now i have added this but still blank?

-snip-

Nope. You don't need to set $host. You need to call the function with a variable. Like this:

echo ping("8.8.8.8");
Put that line outside of the function. It should look like this:

2015-05-12_1604.png

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949619
Share on other sites

Link to post
Share on other sites

Nope. You don't need to set $host. You need to call the function with a variable. Like this:

 

echo ping("8.8.8.8");
Put that line outside of the function. It should look like this:

2015-05-12_1604.png

 

thanks alot mate for your persistance its now working yippee! now ill have to work on it to do some other stuff! but again thanks a lot!

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/365287-php-help/#findComment-4949656
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×