473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
4 31220
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colin Steadman | last post by:
I have built a page that lists all our databases, the users connected to each database, and what application they are using. Because these databases are moved, or removed from time to time I need...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
5
by: Anthony Robinson | last post by:
Consider the following tables: CREATE TABLE "AIMD "."CHANNELSESSION" ( "CHANNELSESSIONID" DECIMAL(13,0) NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +1 , INCREMENT BY +1 , CACHE 20 ) ,...
2
by: Pawel Wrobel | last post by:
Hi! I have a folowing situation - in my Access database I have a table that is linked with SPS. I put some data into this table to present it no SPS. The thing is that when I have no connection...
1
by: Dmitri Vorobiev | last post by:
Hello group, Is it possible to check the presence of a structure member at compile time using only the C preprocessor? What I have in mind could look like the following. Suppose the...
2
by: Ed P. | last post by:
hello, I want to check to see if a certain registry key exists on the registry. What's the easiest way to do this. Here is what I am currently doing. private string registryKey =...
5
by: sword | last post by:
How can I check whether a file exists?
2
by: RLN | last post by:
Re: Access 2003 I have code to check to see if a table exists. There are several other tables (tblQ12, tblQ13, etc) that do already exist & this code runs fine; msgbox says the value returned is...
3
by: pollygw | last post by:
I have a page that dynamically adds rows to a table and the user can also delete any of the rows in no specific order. When the form is submitted I need to do some validation. I can't loop through...
0
by: MIRRA | last post by:
Hi I have a UNIX script which creates a report. In that script I have to define the java classpath. Also I need to check if the java classpath specified exists or not. Below is the command i use ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.