Jump to content

PHP ping script

Joveice

Hi, I'm unable to make a ping script for PHP to display the latency of the ping. How would I do this?

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

46 minutes ago, Joveice said:

Doesent work just a white page gets displayed

Thank you anyways.

Where should I really place the .php? I made a new folder wp-scripts and added it there

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

There's a few likely answers to your issue one of which is that you may be running PHP in a configuration which requires <?php at the opening of your script. Using <? as an open tag. As a result your page is throwing an error which, depending on your configuration would display a white page (instead of the actual error).

 

Another, you mention where to add your php and that you created a folder named wp-scripts. This leads me to believe that you're attempting to add some code to a Wordpress site. If that's the case then you'll want to add your script to the end of your functions.php file in your active themes folder.

I used one of the examples above to make a shortcode that would ping a site and return results. Here's the code I put in my functions.php

function ping_site ($atts) {
	$host = $atts['host'];
		$output = shell_exec('ping -c1 '. $host);
		echo "<pre>$output</pre>";

}
add_shortcode( 'ping', 'ping_site' );

Here's the shortcode I used on my Wordpress page 

[ping host="http://google.com"]

 

And here is what the resulting page looks like.

pingresult.PNG.83001f6ec5a47da50217385c2

Link to comment
Share on other sites

Link to post
Share on other sites

And If you are in a shared hosting environment your hoster may have disabled curl for security reasons, resulting in error messages like "call to undefined function curl_setopt" or the like.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, zushiba said:

There's a few likely answers to your issue one of which is that you may be running PHP in a configuration which requires <?php at the opening of your script. Using <? as an open tag. As a result your page is throwing an error which, depending on your configuration would display a white page (instead of the actual error).

 

Another, you mention where to add your php and that you created a folder named wp-scripts. This leads me to believe that you're attempting to add some code to a Wordpress site. If that's the case then you'll want to add your script to the end of your functions.php file in your active themes folder.

I used one of the examples above to make a shortcode that would ping a site and return results. Here's the code I put in my functions.php


function ping_site ($atts) {
	$host = $atts['host'];
	if(filter_var($host, FILTER_VALIDATE_URL)){
		$output = shell_exec('ping -c1 google.com');
		echo "<pre>$output</pre>";
		
	}else {
		echo "Is not valid URL";
	}
}
add_shortcode( 'ping', 'ping_site' );

Here's the shortcode I used on my Wordpress page 

[ping host="http://google.com"]

 

And here is what the resulting page looks like.

pingresult.PNG.83001f6ec5a47da50217385c2

No idea I gave up on it anyways since showing the ping to it self dident really do any information. So I added system load instead which is working right now :)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, burki94 said:

And If you are in a shared hosting environment your hoster may have disabled curl for security reasons, resulting in error messages like "call to undefined function curl_setopt" or the like.

Private server :)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

I updated the code there the valid url thing doesn't play well in the ping command and I didn't add it to the string in that example. The output is correct though.

Link to comment
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

×