Wednesday, May 16, 2012    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jan 2006 >>
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Search

Categories
 • Acrobat (5) [RSS]
 • Adobe (117) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (34) [RSS]
 • AdobeMAX11 (28) [RSS]
 • AdobeMAX13 (1) [RSS]
 • AIR (299) [RSS]
 • Appearances (217) [RSS]
 • Books (86) [RSS]
 • CFEclipse (15) [RSS]
 • Cloud (1) [RSS]
 • ColdFusion (1482) [RSS]
 • ColdFusion Builder (23) [RSS]
 • Data Services (43) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (368) [RSS]
 • Flex (565) [RSS]
 • Home Automation (5) [RSS]
 • HTML5 (35) [RSS]
 • JavaScript (3) [RSS]
 • Jobs (133) [RSS]
 • jQuery (15) [RSS]
 • JRun (14) [RSS]
 • Labs (63) [RSS]
 • LiveCycle (37) [RSS]
 • MAX (285) [RSS]
 • Mobile (257) [RSS]
 • PhoneGap (17) [RSS]
 • Regular Expressions (19) [RSS]
 • RIA (21) [RSS]
 • SQL (45) [RSS]
 • Stuff (554) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (167) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
January 2, 2006

ColdFusion User Defined Function: URLExists()

A ColdFusion user asked me for a way to programmatically determine if a URL exists, so I threw together this UDF. It uses <cfhttp> to attempt to retrieve a specified URL (not resolving any internal URLs, and not throwing an error upon failure). If an HTTP status code 404 is returned then the UDF returns false, otherwise it returns true. The code is not just checking for status code 200 (or 2xx, for that matter), and this means that codes like 401 (unauthorized) and 403 (forbidden) will return true (these status codes do not necessarily mean that the URL does not exist).

<!---
Does a URL exist? Checks that host exists and for
404 status code.
--->

<cffunction name="URLExists" output="no" returntype="boolean">
    <!--- Accepts a URL --->
    <cfargument name="u" type="string" required="yes">

    <!--- Initialize result --->
    <cfset var result=true>

    <!--- Attempt to retrieve the URL --->
    <cfhttp method="head" url="#ARGUMENTS.u#"
            resolveurl="no" throwonerror="no" />


    <!--- Check if no status code or status code 404 --->
    <cfif NOT IsDefined("cfhttp.responseheader.status_code")
            OR cfhttp.responseheader.status_code EQ "404">

        <!--- Does not exist, return FALSE --->
        <cfset result=false>
    </cfif>

    <cfreturn result>
</cffunction>

Updated as per Steve's sugestion and Gus' important feedback.

Comments
May I "steal" this for cflib?
# Posted By Raymond Camden | 1/3/06 1:19 AM
You might want to use CFHTTP method="head" instead of the default method="get". Its faster because it only retrieves the headers not the full page content, although it does depend on the the webserver being configured to accept the head method.
# Posted By Steven Erat | 1/3/06 1:50 AM
Ben,

This function is not quite correct. You need to first check that status code is actually returned. If you run your code against a domain that doesn't resolve, you won't get a status code to check and will throw an error. Try it with http://www.benfrta.com

The corrected code should be:

<!--- Does a URL exist? Checks for 404 status code. --->
<cffunction name="URLExists" output="no" returntype="boolean">
<!--- Accepts a URL --->
<cfargument name="u" type="string" required="yes">

<!--- Initialize result --->
<cfset var result=true>

<!--- Attempt to retrieve the URL --->
<cfhttp url="#ARGUMENTS.u#" resolveurl="no" throwonerror="no" />

<!--- Check That a Status Code is Returned --->
<cfif isDefined('cfhttp.responseheader.status_code')>
<cfif cfhttp.responseheader.status_code EQ "404">
<!--- If 404, return FALSE --->
<cfset result=false>
</cfif>
<cfelse>
<!--- No Status Code Returned --->
<cfset result=false>
</cfif>
<cfreturn result>
</cffunction>
# Posted By Gus | 1/3/06 8:54 AM
Steve, Gus, good points, updated.

Ray, sure, go for it.
# Posted By Ben Forta | 1/3/06 11:17 AM
Posted.
# Posted By Raymond Camden | 1/3/06 12:00 PM
May I borrow this code for a short time for use in Geonosis(tm) ? I promise to return it just as soon as it's been enhanced, optimized or otherwise improved. Thx.
# Posted By Ray Horn | 1/4/06 7:52 AM
I realize this post is a few years old, but I still found it and tried to use it. I wanted to make note of a couple of issues I found for future users.

I think the 'NOT IsDefined("cfhttp.responseheader.statuscode") ' should be in parenthesis. Maybe I am just a paranoid programmer (I haven't actually tested it), but I think that having the 'NOT' at the beginning of the statement without putting it into parenthesis would make the not work against the whole statement. For instance the status_code actually returns 404, the 'NOT' would make the whole statement false and the url would be deemed valid, even though it is not.

...We'll see if this makes it past the spam filter... Hopefully my second half will as well...
# Posted By Noah Roberts | 8/10/11 11:47 AM
...Second half...

I ran into another issue on my server. Its DNS provider still sends 200 status code if the URL does not exist. Maybe it is a bug in their code, but I added a check for it. Other DNS providers may have similar issues, so check for a known bad URL first and adapt as necessary. I checked cfhttp.responseheader.server for a "OpenDNS Guide".
# Posted By Noah Roberts | 8/10/11 11:53 AM
For some reason my comment is not showing up, but the second issue was with OpenDNS not returning 404.
<cfif (NOT IsDefined("cfhttp.responseheader.status_code")) OR cfhttp.responseheader.status_code EQ "404" OR (IsDefined("cfhttp.responseheader.server") AND cfhttp.responseheader.server EQ "OpenDNS Guide")>
# Posted By Noah Roberts | 8/10/11 11:55 AM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved