By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,642 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,642 IT Pros & Developers. It's quick & easy.

HOW TO: check if a URL exists or not - ErROR 404 ?

P: n/a
gsb
My php passes through some links. But I want to redirect if the URL does not
exist.
How best to check if a URL is there or not?

Thanks,

gsb
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
gsb <gs*@qwest.net> wrote:
My php passes through some links. But I want to redirect if the URL does not
exist.
How best to check if a URL is there or not?


Do a HEAD request to the URL using a socket, if you get a 200 it's
there. If you get something else it might not be (maybe it's a redirect
(301 IIRC)).

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
gsb
Ah, sorry.
I appreciate your response but it is a bit over me.
I've used sockets only in an example that I modified, and do not know how to
do a 'HEAD request.'
I can not find a reasonable example in the ref. I use.

Could you elaborate a little, or show an example?

If not, thanks anyway.

gsb
"Daniel Tryba" <ne****************@canopus.nl> wrote in message
news:bn*********@news.tue.nl...
gsb <gs*@qwest.net> wrote:
My php passes through some links. But I want to redirect if the URL does not exist.
How best to check if a URL is there or not?


Do a HEAD request to the URL using a socket, if you get a 200 it's
there. If you get something else it might not be (maybe it's a redirect
(301 IIRC)).

--

Daniel Tryba
r

Jul 17 '05 #3

P: n/a
gsb <gs*@qwest.net> wrote:
[use HEAD to check for URL validity]
Ah, sorry.
I appreciate your response but it is a bit over me.
I've used sockets only in an example that I modified, and do not know how to
do a 'HEAD request.'
I can not find a reasonable example in the ref. I use.


It's quite simple, if the url you want to check is
http://hostname.tld/foo:

<?php
if($sock=fsockopen('hostname.tld',80))
{
fputs($sock, "HEAD /foo HTTP/1.0\r\n\r\n");

while(!feof($sock))
{
echo fgets($sock);
}
}
?>

This will echo 'HTTP/1.0 200 OK' if the url exists and might return
'HTTP/1.0 404 Requested URL not found' for a non existant page among
other headers.

You can disect an url with parse_url to get the hostname/port/path from
a URL. You might want to do HTTP/1.1 request (where a Host: header is
required (read RFC 2616 if you want to know more)).

But maybe you should take a look at PEAR, there appears to be an
HTTP::HTTP_Client class that prbably does something like this...

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
Daniel Tryba <ne****************@canopus.nl> writes:
gsb <gs*@qwest.net> wrote:
[use HEAD to check for URL validity]
Ah, sorry.
I appreciate your response but it is a bit over me.
I've used sockets only in an example that I modified, and do not know how to
do a 'HEAD request.'
I can not find a reasonable example in the ref. I use.
It's quite simple, if the url you want to check is
http://hostname.tld/foo:

<?php
if($sock=fsockopen('hostname.tld',80))
{
fputs($sock, "HEAD /foo HTTP/1.0\r\n\r\n");

while(!feof($sock))
{
echo fgets($sock);
}
}
?>

This will echo 'HTTP/1.0 200 OK' if the url exists and might return
'HTTP/1.0 404 Requested URL not found' for a non existant page among
other headers.


What about (e.g.), 300 "Moved Permanently", 301 "Found", 307
"Temporary Redirect", ... ?
You can disect an url with parse_url to get the hostname/port/path from
a URL. You might want to do HTTP/1.1 request (where a Host: header is
required (read RFC 2616 if you want to know more)).
"Host" should definitely be provided, as virtual hosting is quite
common these days.
But maybe you should take a look at PEAR, there appears to be an
HTTP::HTTP_Client class that prbably does something like this...


I believe you can use the curl_* functions as well...

--
Micah J. Cowan
mi***@cowan.name
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.