# PHP script to report broken link



## owaters (Aug 8, 2004)

Hi, 

I am wanting to know if it is possible with PHP to report a dead link. 

Basically what I need to so is for a script to check if a link is dead, if it is then display a certain image, if it isn't then display a different image.
This only needs to occur for one link.

I need to test if my IP address reports a dead link, if it does then it will display an image to say I am offline, otherwise it will display an image to say I am online.

How can I do this? 

Thanks in advance


----------



## Pengu (Aug 8, 2004)

try using the system() command.


```
$ping = system( 'ping 123.45.67.89 1');
```
(the 1 is to represent that it should do one ping)

then just check to see if $ping contains a time.


----------



## liquidg3 (Aug 9, 2004)

In Apache you can set a 404 error page.  Try and set that to a php page that uses $_SERVER['self'] (not sure about that variable but php.net search is down).  That will allow you to recieve an email everytime a link doesn't work and send you the path to it.

If someone has more specific info on how exactly to do this, let us know.


----------



## Pengu (Aug 9, 2004)

I think one of us has mis-understood the original intent...


----------



## 3mors (Aug 10, 2004)

you can use _ping way_ or:

use *file("http://www.domain.com/hello.txt")* function
it returns an error if the file hello.txt is missing
u can also use relative or local paths

by managing the error, u can display if the link is ok or not.


----------



## TommyWillB (Aug 10, 2004)

Pengu said:
			
		

> I think one of us has mis-understood the original intent...


That's what I was just thinking...

 owaters can you tell us what you mean by "dead"?

 Are you testing one specific URL, and entire server, or a bunch of URL's on a site?


----------



## Pengu (Aug 10, 2004)

The reason I suggested ping, is that he may not be running apache on his local machine, and it seems stupid to run apache just to find out if his machine is turned on/connected


----------



## 3mors (Aug 11, 2004)

Pengu said:
			
		

> The reason I suggested ping, is that he may not be running apache on his local machine, and it seems stupid to run apache just to find out if his machine is turned on/connected



sorry but i dont understand
why apache?
php functions work on every webserver that runs php language

and if u want to be preciser, ping way works only if u have ping command on computer and if u have permissions to run system commands

if u put the script on a remote webserver (not managed by u) probably it doesn't work.


----------



## Pengu (Aug 11, 2004)

Ok.

the other suggestions involved trying to open an http connection to a file on his local machine. that requires Apache to be running. seems like a bit of overkill just to check if his computer is on. and you could just use the php_info() command to establish what the safe mode "exec" directory is, and put a copy of the ping binary there. and it is my experience that most services have safe mode turned off by default.


----------



## 3mors (Aug 11, 2004)

Pengu said:
			
		

> Ok.
> 
> the other suggestions involved trying to open an http connection to a file on his local machine. that requires Apache to be running. seems like a bit of overkill just to check if his computer is on. and you could just use the php_info() command to establish what the safe mode "exec" directory is, and put a copy of the ping binary there. and it is my experience that most services have safe mode turned off by default.



ok, i've understood now
but if someone talk about a link, i think of http link so a webserver (apache, iis, etc etc) has to be run

anyway if u want to check if, for example, a quake3 server is up, u could use fsockopen() function

ping is a good way, but the response in some cases arrives only after one or two seconds (if the server is down) and it's not a good thing in a web page

and what do u do if ping is blocked by the remote server?
for example, my webserver is up but my router blocks ping requests


----------

