Ping ip in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Ping ip in php

Post by GeXus »

What is the best way to ping anIP in php and determine if it is live or not?

Thanks!
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

PEAR Net_Ping will do the job
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Ping ip in php

Post by Chris Corbyn »

GeXus wrote:What is the best way to ping anIP in php and determine if it is live or not?

Thanks!
PHP doesn't have even the slightest ability to send icmp packets (ping).

The best you can do is either:

a) Try to use fsockopen() on a certain port if you're only needing to know about a given port
b) Execute ping via the shell commands

;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

wtf wrote:PEAR Net_Ping will do the job
:) Cool.

Does that use shell commands or native PHP code? Sorry, I havent looked at it but figured you might already know.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

You can always use this (if its what your trying to accomplish...

Code: Select all

if(!fopen("http://www.your-url_or_your-ip-here","r")){

Take some action! (IE -- Page yourself)

}
Hope that helps!
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

d11wtq wrote:Does that use shell commands or native PHP code? Sorry, I havent looked at it but figured you might already know.
It exec's shell commands.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

siefkencp wrote:You can always use this (if its what your trying to accomplish...

Code: Select all

if(!fopen("http://www.your-url_or_your-ip-here","r")){

Take some action! (IE -- Page yourself)

}
Hope that helps!
That would, at best, tell you if the URL or IP responds to HTTP requests. Failing does not mean that it does not exist or isn't live unfortunately.
Post Reply