Using URL Variables ColdFusion Help

If you have done any amount of Web browsing, you have probably encountered long URLs with long strings of parameters following a question mark in the following form:

These URLs are built out of a basic URL (http://server /path/document) and a series of parameters of the form paramet er=va’l ue. Each parameter is separated from the next by an ampersand (&), and the series of parameters is separated from the first part of the URL containing the server, path, and document by a question mark (?).

URL parameters are extremely powerful because they allow the passing of information between pages in a site through their links. This is not a browser-dependent feature’

(as are other techniques for-passing information between pages, such ascookies which you will look at next). For this reason, many sites rely on URL parameters as the primary mechanism for moving data between pages of a site.

The need to use URL parameters to move data between pages arises from a fundamental feature of the World Wide Web and its protocol, Hypertext Transfer Protocol (HTTP). HTTP is a stateless protocol. This means it is centered around requests: A client requests a particular URL the resulting data is sent to the client and the transaction is finished.

The stateless nature of HTTP means that when the user clicks a link in the page that was just sent, an entirely new request is initiated that the server has no way to associate with the previously fulfilled page. The use of URL parameters provides one means to pass information between pages and allows server-side programming environments such as ColdFusion to use the information in building the pages that are returned to the user.

Accessing these URL parameters in your templates is surprisingly easy: Simply add URl. to the front of the parameter name you want to reference. For instance suppose you have a link to a template called url . cfm, like this:

<A HREF-‘url.cfm?Info-Information’)

In the u r 1 . cfm template, the following code:

<CFOUTPUT>#URL.Info# was received</CFOUTPUT>

would result in the following line of output:

Information was received

Given this capability to easily pass information along from page to page in a site ColdFusion can be used to easily build dynamic links containing parameters. For instance, in url. cfm, you could use the following code to pass the received information along to another page:

<CFOUTPUT><AHREF-‘ur12.cfm?Info-#URL.Info#’)</CFOUTPUT?

As would be expected, the resulting output is written as follows

<A HREF-‘u~12.cfm?Info-Information’>

Limitations of UR Parameters

For all their utility URL parameters do have limitations These limitations are imposed by restrictions on what is allowed in a valid URL.These restrictions include the inability to use several characters including spaces

This limitation is addressed by something known as URL encoding. URL encoding involves translating any special characters in a URL such as spaces and equal signs into two-digit hexadecimal (base 16) codes of the form %XXI where XX is the two-digit number.

Of course it would be possible to manually translate the values of your URL parameters to URL-encoded format but this would be difficult and time-consuming. Instead Cold fusion provides the URL Encoded Format function to perform the conversion for you. This function takes a ‘single string argument. It returns the strings in URL-encoded format.

By way of example, the following code outputs the string Thi 5 is a test in URL ended format:

<CFOUTPllT>fURLEncodedForll’at(“This is a test’)I#</CFOUTPUT>

The results look like those in Figure 4.2.

FIGURE 4.2

FIGURE 4.2

Given this you can build your dynamic URLs with URL-encoded parameters. In the earlier example where you built a dynamic URL with the following:

<CFOUTPUT><AHREF~·ur12.cfm?Infoa#URL.lnfo#·></CFOUTPUT>

you can add URLEncodedFormat so that regardless of the value of the Info URL pararn- I eter, you can be sure that the resulting URL is valid:

<CFOUTPUT><AHREF-“ur12.cfm?Info-#URLEncodedFormat(URL.lnfo)#’></CFOUTPUT>

Posted on November 14, 2015 in Creating and Manipulating Variables

Share the Story

Back to Top
Share This