Have a single variable URL in ColdFusion

Some websites have URLs with only a question mark and one variable, e.g. http://www.blah.com?123 or http://www.blah.com/index.cfm?123.

You can do this with ColdFusion by using CGI.QUERY_STRING. This gives you all of the information after the ? in the URL. As in this case there is only one piece of information you can use it to look up a record in your database.

E.g. in the case of looking up a number as the ID:

URL: http://www.tenpastmidnight.com?123

<CFQUERY NAME="DBRead" DATASOURCE="#Datasource#">
   SELECT Data
   FROM DataBaseName
   WHERE ID = #val(CGI.QUERY_STRING)#
</CFQUERY>

The 'val()' makes sure that the database looks up a number, rather than some nefarious code, see other CF security tips.

More code articles

Useful books
Cover of Mastering ColdFusion
Mastering ColdFusion MX
Amazon UK | USA
Cover of Programming ColdFusion MX
Programming ColdFusion MX
Amazon UK | USA
Cover of ColdFusion X Web Application Construction Kit
ColdFusion X Web Application Construction Kit
Amazon UK | USA

Paul Silver. February 2004


Silver Web Services Ltd

© 2017 Paul Silver