473,372 Members | 1,135 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,372 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 31215
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.