Skip to content

Instantly share code, notes, and snippets.

@JamoCA JamoCA/NewLocation.cfm
Last active May 18, 2017

Embed
What would you like to do?
CF_NewLocation - Duplicate of built-in ColdFusion CFLocation function but with added ability to prevent penalty when using rate limiter
<CFSETTING ENABLECFOUTPUTONLY="Yes">
<!--- 5/18/2017 NewLocation Check and reduce Optional rate-limiter prior to redirect
Rate Limiter UDF that uses CFCache: https://gist.github.com/JamoCA/f1af7397d92b93eeea20
Use just like tag-based CFLocation tag https://cfdocs.org/cflocation
<CF_NewLocation url="http://localhost:8500/cfdocs/dochome.htm" addToken="no"> --->
<CFPARAM NAME="Attributes.URL" DEFAULT="/">
<CFPARAM NAME="Attributes.AddToken" DEFAULT="no">
<CFPARAM NAME="Attributes.statusCode" DEFAULT="302">
<CFSET BrowserKey = "#CGI.Server_Name#_#CGI.Remote_Addr#_#CGI.Http_User_Agent#">
<CFSET CurrentLimiter = cacheGet(BrowserKey)>
<CFSET CacheConfig = {}>
<CFIF NOT listFind("300,301,302,303,304,305,307", Attributes.StatusCode)>
<CFSET Attributes.StatusCode = 302>
</CFIF>
<CFIF NOT isValid("boolean", Attributes.addToken)>
<CFSET Attributes.addToken = "no">
</CFIF>
<CFIF NOT isNull(CurrentLimiter)>
<CFSET CurrentLimiter.attempts = CurrentLimiter.attempts - 1>
<CFIF CurrentLimiter.attempts LT 1>
<CFSET CurrentLimiter.attempts = 1>
</CFIF>
<CFSET CacheConfig = cacheGetMetadata(BrowserKey)>
<CFSET cachePut(browserKey, CurrentLimiter, CreateTimeSpan(0, 0, 0, CacheConfig.timespan))>
</CFIF>
<CFSETTING ENABLECFOUTPUTONLY="No">
<cflocation addtoken="#Attributes.addToken#" statuscode="#Attributes.StatusCode#" url="#Attributes.URL#">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.