Find a Broken Link - Coldfusion

AID: 1079
  • Status: Published

2510 points

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not?

For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show you how to accomplish this task programmatically within your CFML-based web application.


The Code
<cffunction name="urlExists" output="no" returntype="boolean">
    <!--- Accepts a URL --->
    <cfargument name="urlval" type="string" required="yes">
 
    <!--- Initialize result to FALSE unless returns valid status --->
    <cfset var result=false>
 
    <!--- Attempt to retrieve the URL with method head  to get only the header info in response --->
    <cfhttp url="#arguments.urlval#" method="head" resolveurl="no"  timeout="1000" />
 
    <!--- Check That a Status Code is Returned --->
    <cfif isDefined("cfhttp.responseheader.status_code")>
        <cfif cfhttp.responseheader.status_code NEQ "404">
            <!--- If NOT 404, return TRUE --->
            <cfset result=true>
        </cfif>
    </cfif>
 
    <cfreturn result>
</cffunction>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window



The function requires that the developer passes in a URL as a parameter and then returns a boolean flag indicating the existence of the specified URL.

For improved performance, the function hits the url using CFHTTP and HEAD method to get only the header status code as a response, rather than retrieving all the page information which we would using GET method.  Thus the process becomes much faster.


The Usage
<!--- set a variable like chkurl to any URL. --->
<cfset chkurl = "http://your.url.here" />

<!--- calling the function urlExists and passing the chkurl value to the function within a cfif conditional --->
<cfif not urlExists(chkurl)>
    <!--- broken link, display alternate information. --->
<cfelse>
    <!--- link valid, continue processing requiring the checked URL. --->
</cfif>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window



As simple as that!

And you can even put this function in a ColdFusion Component (CFC), allowing it to be used in any application you want.


The Need
But why would we even want to use this in an application?

Well, just consider the power of the CFHTTP tag utilized above.  It can be used to create a portal page that presents data from other portions of your intranet for example.  

Consider now on this page, you show information from each of five departments, who have their own web page.  What if one of their pages was no longer valid/down?  

With this neat new function, you will be able to determine in code, quickly, if a link is broken and display an error or alternate content that is more appropriate to your page design.

Moreover, since this is done dynamically, if that URL ever becomes valid again, then your code doesn't need to be changed.  The other condition will now be executed as the link is no longer broken.


Hope you found this helpful and it serves you well!


For more information on CFHTTP tag:
http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p58.htm
Asked On
2009-06-30 at 04:22:05ID1079
Tags

Coldfusion

Topic

ColdFusion Application Server

Views
1925

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top ColdFusion Server Experts

  1. Zvonko

    2,800

    0 points yesterday

    Profile
    Rank: Genius
  2. srikanthmadishetti

    2,580

    20 points yesterday

    Profile
    Rank: Guru
  3. digicidal

    2,000

    0 points yesterday

    Profile
    Rank: Guru
  4. maestropsm

    1,600

    0 points yesterday

    Profile
    Rank: Guru
  5. _agx_

    1,268

    0 points yesterday

    Profile
    Rank: Genius
  6. gdemaria

    1,064

    0 points yesterday

    Profile
    Rank: Genius
  7. brijeshchauhan

    668

    0 points yesterday

    Profile
    Rank: Guru
  8. myselfrandhawa

    250

    10 points yesterday

    Profile
    Rank: Guru

Hall Of Fame