<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>
<!--- 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>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)