Useful CGI Variables
Cold Fusion makes it easy to interact with the "Common Gateway Interface" that
the web is built on. There are many usefull applications for the information
stored in "CGI" variables.
First, a list of the CGI variables Cold Fusion makes available to you.
Cold Fusion CGI | Output |
CGI.server_software | Microsoft-IIS/5.0 |
CGI.server_name | www.cfhub.com |
CGI.gateway_interface | CGI/1.1 |
CGI.server_protocol | HTTP/1.1 |
CGI.server_port | 80 |
CGI.server_port_secure | 0 |
CGI.request_method | GET |
CGI.path_info | /examples/cgi/index.cfm |
CGI.path_translated | e:\web\cfhub\examples\cgi\index.cfm |
CGI.script_name | /examples/cgi/index.cfm |
CGI.query_string | |
CGI.remote_host | 81.137.208.120 |
CGI.remote_addr | 81.137.208.120 |
CGI.auth_type | |
CGI.remote_ident | |
CGI.content_type | |
CGI.content_length | 0 |
CGI.http_referer | |
CGI.http_user_agent | Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6 |
These CGI variables can be used in in many ways, but here two common ones:
CGI.http_referer, by itself, is the primary ingredient for
the ultimate "Back Button". No matter where a person was on your
site, when they click this "Back" link, they will be returned there.
<a href="<cfoutput>#CGI.http_referer#</cfoutput>">GO BACK</a>
|
In this example, you see that the <cfoutput> tag can be used inside other HTML tags.
*.cfm pages are pre-proccessed by the Cold Fusion Application Server first, so you can
use Cold Fusion inside other tags, or to generate JavaScript arrays etc. Also
remember that <cfoutput> tags cannot be nested.
CGI.Remote_Host, CGI.path_info, and CGI.http_referer make up
a good part of the information you would be interested in tracking
for a "hit" counter. Add a little script to the to of your Cold
Fusion Pages that adds that data to a dbase, and then you can
tell who came to what pages, and from where. Add a Timestamp,
and presto. Your first simple Web Tracker Tool. To see an
example of what this might look like:
<cfset hit_time = #CreateODBCDateTime(Now())#>
<cfquery datasource='YourDataSource'>
Insert into TrackingTable(Visitor_IP,Came_To,Came_From, hit_time)
Values('#CGI.remote_host#','#CGI.path_info#','#CGI.http_referer#',hit_time)
</cfquery>
|
For this example to work for you, YourDataSource must be
the name of an ODBC data source you have set up ponting to a database that
has a table called TrackingTable, that in turn must have
the fields [text (50) should be OK] Visitor_IP, Came_To, and Came_From. Every
page that has the script on it will log all the traffic that comes to it
in the dbase.
Another part of this script that may give you trouble, is the Native
Cold Fusion Funtion used to set the "time" of the update.
CreateODBCDateTime() converts a date into a format
that databases can understand and use. In this case we nested another
function Now() inside. Now() just returns a Date/Time
object representing ...Now. Combining these two functions is one way
to create ODBC date/time objects suitable to insert into databases.
|