Skip to main content
August 12, 2013
Question

Empty User Agent string checking with RegEx fails

  • August 12, 2013
  • 1 reply
  • 798 views

Why does this not work for the User Agent empty string?

<cfif REFindNoCase("^$", CGI.HTTP_USER_AGENT)>

   <cfoutput>

      "#CGI.HTTP_USER_AGENT#"

   </cfoutput>

</cfif>

For obvious reasons this works as an alternative.

<cfif ! len(CGI.HTTP_USER_AGENT)>

   <cfoutput>

      "#CGI.HTTP_USER_AGENT#"

   </cfoutput>

</cfif>

However, I'm not really interested in a workaround but the reason why the firt RegEx approach fails.

Testing conducted with FF Add-On "User Agent Switcher" I'm able to impersonate a browser that is void of a UA.

Other info:

Adobe ColdFusion 9.0.1

Server 2008 R2 64 bit

Testing code from within application.cfm

This topic has been closed for replies.

1 reply

p_sim
Participating Frequently
August 12, 2013

I would use "^.+" instead of "^$"

August 12, 2013

"^.+" should be the same as ".{1,}" which will match any single character that is not a line break character between one and unlimited times (greedy).

That also would essentially match just about anything which makes it very useless for matching since it would definitely pollute with false positives.  Thanks for the assist though.