Skip to content

Instantly share code, notes, and snippets.

@aqlong
Created April 20, 2010 15:09
Show Gist options
  • Save aqlong/372601 to your computer and use it in GitHub Desktop.
Save aqlong/372601 to your computer and use it in GitHub Desktop.
<!---
This customTag allows you to override cflocation and debug the page
*before* it redirects (very helpful for development & debugging
difficult bugs that occur on the page which calls cflocation)
Usage:
1) create a Request variable called overrideRedirect that you can access
with via Cookie or URL and do something like the following in your
onRequestStart() in your Application.cfc
<cfif StructKeyExists(Cookie, "overrideRedirect")>
<cfset Request.overrideRedirect = Cookie.overrideRedirect>
<cfelse>
<cfset Request.overrideRedirect = False>
</cfif>
2) replace all your cflocation calls with something like the following
<cfmodule template="#Application.tagDir#redirect.cfm"
url="/Login.cfm?MessageCode=181"
overrideRedirect="#Request.overrideRedirect#">
OR
<cf_redirect
url="/oldPage.cfm"
overrideRedirect="#Request.overrideRedirect#"
statusCode="302">
--->
<cfif ThisTag.ExecutionMode IS "Start">
<cfparam name="attributes.url" type="string" default="">
<!--- addToken is True by default in cflocation, but I prefer false in most situations --->
<cfparam name="attributes.addToken" type="string" default="false">
<!--- "300|301|302|303|304|305|307" --->
<cfparam name="attributes.statusCode" type="string" default="302">
<cfparam name="attributes.overrideRedirect" type="string" default="false">
<cftry>
<!--- get addToken if possible; otherwise blank --->
<cfif StructKeyExists(Session, "addToken")>
<cfset variables.URLTokenString = Session.addToken>
<cfelseif StructKeyExists(Client, "addToken")>
<cfset variables.URLTokenString = Client.addToken>
<cfelse>
<cfset variables.URLTokenString = "">
</cfif>
<cfcatch type="application">
<cfset variables.URLTokenString = "">
</cfcatch>
</cftry>
<cfset variables.url = attributes.url>
<cfif attributes.addToken>
<cfif Find("?", attributes.url)>
<cfset variables.concatCharacter = "&">
<cfelse>
<cfset variables.concatCharacter = "?">
</cfif>
<!--- add token if its not already there --->
<cfif NOT Find("CFTOKEN", attributes.url)>
<cfset variables.url &= "#variables.concatCharacter##variables.URLTokenString#">
</cfif>
</cfif>
<cfif attributes.overrideRedirect>
<cfoutput>
attributes.url=[#attributes.url#]<br />
attributes.addToken=[#attributes.addToken#]<br />
variables.URLTokenString=[#variables.URLTokenString#]<br />
variables.url <a href="#variables.url#">#variables.url#</a>
</cfoutput>
<cfabort>
<cfelse>
<cflocation url="#attributes.url#"
addtoken="#attributes.addToken#"
statusCode="#attributes.statusCode#" />
</cfif>
</cfif>
@aqlong
Copy link
Author

aqlong commented Apr 28, 2010

fixed bugs in the way the addToken was added to the URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment