Always get the correct URL path

If you using a development methodology you have perhaps run into one the following situations: When crating a form that should be submitted to it self, or using a named anchor on your page and when you click on submit or the named anchor you get an error 'The page cant be found' or 'another page came up'.

To avoid those problems create a variable that always gives the correct URL path, even if you running on local host. 
  
ColdFusion has many different scopes of variables. Some remain in RAM others exist only on a specific page. For most of your global variables our recommendation is using the request scope. The request scope is similar to the application scope, except that it does not remain in memory. 

In this example we will create a request scope variable in the ColdFusion application file. In that way the variable can be accessed anywhere in your application.

Enter this code in the coldfusion application file:
<cfset request.UrlStr = "http://"&#CGI.SERVER_NAME#&#CGI.PATH_INFO#>

 

About the two CGI variables:
CGI.SERVER_NAME:
The server's hostname, DNS alias, or IP address as it would appear in self-referencing URLs.

Running a test on this CGI variable will return the following:
<cfoutput>#CGI.SERVER_NAME#</cfoutput>
On local host (127.0.0.1) the output return: localhost
On the server: The output return:
www.applayit.com

 
CGI.PATH_INFO
Extra path information added to the end of the URL that accessed the server side script program.

Running a test on this CGI variable will return the following:
<cfoutput>#CGI.PATH_INFO#</cfoutput>
On local host (127.0.0.1) the output returns: /applayit/name_of_the file.cfm
On the server: name_of_your_file.cfm

 
Here?s an example using this global variable on a page using a named anchor:
<a href="<cfoutput>#request.UrlStr#</cfoutput>/#Top">Top of Page</a>

Advertisement

Internet Million Dollars

User Comments: 3

 Good ! Much Useful

It's the same I was looking for it !

Thanks !!

 Wonderful

Thanks for making coding easy instead of fancy and difficult.

 A Step Above The Rest

Thanks for the handy little tut.

I was using another script that somebody else created:

<cfscript>
     // this function gets called to determine the form actions.
     function getURL() {
          theURL = CGI.script_name;
          if(CGI.query_string NEQ '') {
               theURL=theURL & '?#CGI.query_string#';
          }
          return theURL;
     }
</cfscript>

What a lot of code in comparison to your cfset tag!

ApplayIT is owned by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.