Displaying a Custom Greeting Based on a User s Referring URL

 < Day Day Up > 



Displaying a Custom Greeting Based on a User's Referring URL

When users visit your site via a link somewhere else on the Web, it's often useful to know where they came from (their referring URL). For example, your online store might offer a discount to users who came from a certain advertiser's site; or it may simply display a custom greeting acknowledging where the user came from. To build actions like this into a ColdFusion MX template, your code needs to be able to make a decision. It needs to first determine where the user came from and then perform one or more actions based on that determination.

First, you need to know a little about a special CGI environment variable called http_referer. Although it's not generated by ColdFusion MX itself, it is available within any ColdFusion template.

Tip 

CGI environment variables are not a function of ColdFusion MX — rather, they're present any time a communication occurs between a browser and a Web server. They're used in a variety of Web programming languages to get information including the user's browser type (http_user_agent), the user's IP address (remote_addr), or the name of the Web server on which a page resides (server_name). For a complete list of such variables, do a Web search for the term "CGI environment variable."

Start by creating a new Dreamweaver site called "Examples." Then start a new dynamic ColdFusion page within it, naming your template  check_referral.cfm. Add the following code within your template's body tags:

<cfoutput>#http_referer#</cfoutput>
Note 

The spelling of referer in http_referer is incorrect in the English language, but it remains so as a holdover from the early days of the Web.

Now create a second page, this time just a standard HTML document. Save the page as links1.htm, in the same folder as  check_referral.cfm. Place the following code within the body tags:

<a href="check_referral.cfm">Click here to view check_referral.cfm</a>

Now preview links1.htm in your browser and click the link you created. Your template  check_referral.cfm displays the URL of the referring page, as seen in Figure 49-1.

click to expand
Figure 49-1: The output of the http_refer variable in a ColdFusion MX template

You might see a slightly different URL depending on where your pages are stored. In this case, the referring URL is a local page that resides on your server, but http_referer also works if the referring page is at a remote location, say someone else's Web site. In this example, you're using links1.htm to test your work because it's much easier than calling up a friend with a Web site and asking her to post a link to  check_referral.cfm.

Next you need to click File®Save As in Dreamweaver to make two copies of links1.htm, named links2.htm and links3.htm. Load each in your browser, click the link, and your ColdFusion template displays the URL corresponding to links1.htm,  links.htm, or links3.htm, depending on which page you were viewing when you clicked the link.

Tip 

As a CGI variable, http_referer may also be named using the CGI scope: cgi.http_referer. For more on variable scopes, see Chapter 48.

You're now able to track and display where your users have come from. The next step is to provide a custom greeting based on the page from which the user arrived. You use <cfif> and its companion tags to do this by adding some new code to  check_referral.cfm, as shown in Listing 49-1.

Listing 49-1: check_referral.cfm

start example
 <html> <head> <title>Check Referral</title> <meta http-equiv="Content-Type" content="text/html; charset=iso- 8859-1"> </head>     <body> <h2>Welcome To Our Site</h2>     <cfif http_referer contains 'links1'>      <p>We notice you're visiting us via a link on links1, which is one  of our most valued advertisers. We appreciate you taking the time to  look around and hope you have a valuable experience while you're  here.</p>     <cfelseif http_referer contains 'links2'>      <p>We notice you're visiting us via a link on links2, a site run  by our good friend Jim Smith. Please be sure to tell Jim what you  thought of our site.</p>     <cfelseif http_referer contains 'links3'>      <p>We notice you're visiting us via a link on links3, a site we no  longer wish to be associated with. If you speak to your friends about  our site, please don't do it in the same breath with links3.</p>     <cfelse>      <p>Thanks for visiting. Please look around and mail us if you have  any questions.</p>     </cfif>     </body> </html>
end example

After you add this code,  check_referral.cfm acts on the contents of http_referer rather than simply displaying it literally. It includes a <cfif> block, the section of code beginning with <cfif> and ending with </cfif>. Within the block are four conditions: one for each of the links pages, and one default condition for instances when the user has arrived at your site via a link unknown to you.

Note 

When you use a variable name as part of a <cfif> tag, you don't have to enclose it within hash marks. For example, #http_referer# becomes http_referer. This is the case with most CFML tags, although a few exceptions are covered in later chapters.

Let's take a closer look at the code in Listing 49-1. The template's body section begins with a header, Welcome To Our Site, which is placed outside the <cfif> block because the header remains the same regardless of how the user has arrived. Next is the opening <cfif> tag, along with the first condition. The condition states that the environment variable http_referer must contain the string links1. If the condition is satisfied, all text within the first <cfif> and the first <cfelseif> is displayed to the user. If the condition isn't satisfied, the text doesn't display, and ColdFusion MX's processor moves on to the next condition, and so on.

In the condition http_referer contains 'links1', contains is just one of several tests you may use to create a condition. The others are shown in Table 49-1.

Table 49-1: Tests Used in <cfif> Conditions

Test

Description

Examples

is or eq

Determines whether two strings or numbers are identical

<cfif name is 'John'>

<cfif count eq 12>

  

is not or neq

Determines whether two strings or numbers are not identical

<cfif name is not 'John'>

<cfif count neq 12>

  

contains

Tests for a substring within a string; used with non-numeric data

<cfif address contains'Ave'>

does not contain

Determines whether a substring does not occur within a string; used with non-numeric data

<cfif address does not contain 'Ave'>

lt

Less than

<cfif age lt 21>

lte

Less than or equal to

<cfif age lte 21>

gt

Greater than

<cfif days gt 30>

gte

Greater than or equal to

<cfif days gte 30>

Tip 

It's good programming practice to indent any code or HTML text that appears within a <cfif> block to make it easier for you or other developers to identify your template's program flow from its source code. Also, as your templates grow in complexity and you begin to use nested <cfif> blocks, indented code is essential in identifying parent and child blocks.

The second CFML element within the <cfif> in Listing 49-1 is a <cfelseif> tag. This tag essentially states, "if the previous condition wasn't satisfied, then try this one." It differs from <cfelse> because it is always used with a condition; whereas <cfelse> simply says, "if none of the above conditions were satisfied, do this."

Listing 49-1 uses <cfelse> as the last item in the <cfif> block because you want to create a default condition that occurs if none of the others are satisfied. Users who visit the page from a link other than those matched in the conditions see a generic "Thanks for visiting" message rather than a custom one.

When you enter code like Listing 49-1 into Dreamweaver's code box, the design box displays the text for all the conditions, but it uses a ColdFusion element icon to denote the entire <cfif> block, and another to show where each condition occurs, as shown in Figure 49-2.

click to expand
Figure 49-2: Viewing a <cfif> block in Dreamweaver's Design View

Now try loading each of the three links pages and clicking through to  check_referral.cfm. You'll see how <cfif> causes your template's behavior to change depending on the referring URL. One example is shown in Figure 49-3, which appears when you click through from the page links3.htm.

click to expand
Figure 49-3: The output of one of four conditions in check_referral.cfm

Caution 

Versions of ColdFusion prior to MX display an error if your template refers to http_referer and the variable is not defined. This situation occurs when users type your page's address in their browser's URL window or use a bookmark — both cases in which there is no referring URL. If you're using an older version of ColdFusion, you can guard against this problem by testing for the presence of http_referer with a function called IsDefined(). You'll learn more about functions in the next chapter.



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net