Supporting If-Modified-Since HTTP header in CFML
If-Modified-Since header in the HTTP protocol allows user agents (typically RSS aggregators, or Spiders) to check and see if the content they are about to download has changed since their last visit. The user agent sends something like this in the headers:
If-Modified-Since: Fri, 18 Feb 2005 19:24:26 GMT
You can access this header from CFML, compare the date your content was last changed, and then if the content was not modified since the date passed, you return a
304 status code. If the content has been modified, you just return the content as you normally would. Here's some example CFML you can put at the top of your page, you need to update the
<cftry> <cfif StructKeyExists(GetHttpRequestData().headers, "If-Modified-Since")> <cfset ifmodDate = ParseDateTime(GetHttpRequestData().headers["If-Modified-Since"])> <cfset pageModificationDate = GetHttpTimeString(ParseDateTime(Now()))> <cfif DateDiff("n", pageModificationDate, ifmodDate) GT 0> <cfheader statuscode="304" statustext="Not Modified"> <cfabort> </cfif> </cfif> <cfcatch type="any"></cfcatch> </cftry>
I use the GetHttpRequestData() function to get the value of the
If-Modified-Since header (notice that I have to use StructKeyExists instead of IsDefined due to the dash - in the header name).
Also note that the dates need to be formatted properly, use the GetHttpTimeString() function for this.
I also put in a try/catch block to catch any date parsing errors, that way if there is an error parsing a date sent by a bad client, they still get the content, you may decide to log the error, or give a 500 level status code to such clients.
I implemented this with my RSS feed for this blog, so I should see a decrease in bandwidth on my RSS feed. Well written aggregators will only get content when my feed is updated, and just the headers otherwise.
- If-Modified-Since and CFML Part III - February 22, 2005
- If-Modified-Since and CFML Part II - February 18, 2005
- 8 Ways to Save Bandwidth on your RSS Feed - July 12, 2007
- Parsing RSS 1.0 with ColdFusion MX - April 8, 2004
- RSS and XPath - April 8, 2004
I am SEO & i address this issue of not having If modified since in my clients website made with coldfusion. I found this article as a solution and asked the developer to do it & he did. Now when i check if modified since using one tool at feedthebot.com/tools/if-modified.
I can see current date and time as last modified, but my website is not uploaded in last five minutes. I tryed doing it again, again same time & date.
It is not showing last date and time my website was updated. I doubt SE may not consider me as Back hat. Please give your review, code i have used <CFHEADER NAME="If-Modified-Since" VALUE=#GetHttpTimeString(ParseDateTime(Now()))#>
my websites is www.drivingoz.com i am thinking to remove this code, what do you say??
- What is the difference between ASCII Chr(10) and Chr(13)
- Fixinator and Foundeo Security Bundle
- Running CFML on AWS Lambda with FuseLess Slides
- Updating Java on ColdFusion or Lucee
- ColdFusion returning empty response with server-error: true
- Careful applying CF11u16, CF2016u8, CF2018u2
- Sessions don't work in Chrome but do in IE
- csrfVerifyToken does not invalidate the token