ColdFusion Tips and Tutorials

ColdFusion Tips and Tutorials. Tips on ColdFusion, AJAX, CSS, JavaScript, HTML, Design, and more.

CFUnited Developer Conference 2010
Use this code TIPSCUST to get $100 off your registration @ CFUnited! We'll see you There!


ColdFusion Tips
Page 1 2 3
148 ColdFusion, Ajax, FuseBox, Tips, and Tut
147 Included Point of View
146 Javascript - OnFocus
145 Nathan's Rules of Professional Web Desig
144 Universal Server-Side Check | Bandwidth
143 Meeting Schedule | Identification Sessio
142 Breaking Frames Without Javascript
141 Unreal Forms
140 Screen Resolution
139 Human Help
138 Better Server-Side Validation
137 Automatic Server-Side Validation
136 Regular Expression Laboratory,ColdFusion
135 Rank-Ordered Site Search
134 Building Cryptograms
133 Well-Formed Includes
132 Grouping Families for Visits
131 Display Families on a Map Grid
130 Slide Shows
129 Determine Your Database Engine
128 ColdFusion in Context: Maxlength Lies
127 Something Extra
126 Parsing Database Structure from Data Def
125 Valid Values Maintenance
124 Print 1
123 Hide Session Id
122 Downsizing Data to Access
121 Time to Load a Page, FuseBox 4
120 Order and Rank by Subset
119 Warn through E-mail & Update on Paste Sp
118 Paste Spreadsheets, ColdFusion Component
117 Review Files Having Fixed-Length Fields
116 Organized Help
115 Sequence Slider
114 Bad Bits
113 Logical Deduction
112 Whiteout
111 Forced Navigation
110 Managing Permissions
109 Time Travel
108 Test First
107 Get Distance Between Map Coordinates
106 Validating Checkboxes
105 Matrix Manipulation
104 Field Help
103 Fake Object Not Found
102 Rank Order Correlation Coefficient
101 From Calling Pairs to Calling Tree
100 Posting Notice
99 Logout Persuasion
98 Release Session Memory
97 Use Identically Named Fields
96 Web Bug
95 Password Generation
94 Core Queries
93 Use CFFTP
92 Insert, Update, and Delete
91 Stack
90 T-Value
89 Bulk Data Entry and E-mail Validation
88 Quick Reset
87 Design 1
86 Use CFFTP
85 Support Login with AutoPost
84 Login and Site Protection
83 XY Graphs in a Graphing Calculator
82 Read Encrypted Files
81 Showing Progress
80 Frugal Cross-Browser Javascript
79 Tabbed Folders
Page 1 2 3



Custom Search

 

ColdFusion in Context: Included Point of View

By =Marty=

Suppose you include a page. Suppose that page includes an image. Whose point of view counts? The perspective of the calling page, or the perspective of the included page? What effect does this have on you, and what can you do about it?

If you guess wrong, all of your relative references (links, images, form actions, redirects, and page includes) will be broken.

Assuming that a change in perspective wouldn't be noticed if all your pages were in a common directory, we'll instead look first at nested directories and then at sibling directories.

Nested Directories

Start by creating a home directory; call it homenot (because you don't want to follow this example). Within that directory, create a login directory; call it login. Within the homenot directory, place an image of your choice. Then create a rudimentary home page that uses your image (instead of earth.gif below): homenot/index.cfm.

 

<h2>Welcome to my world</h2>
<a href="login/mystery.cfm">
<img src="earth.gif" border="0"></a>
Life is a mystery
<cfif isDefined("cookie.TICKET")>
  <cfif cookie.TICKET contains "_a">
    and love holds the key
  </cfif>
</cfif>

Within the login directory, create mystery.cfm (homenot/login/mystery.cfm) and give it this code. It wouldn't take much to extend this to use an encrypted cookie and a database lookup instead of the fake items used here, but this code just sets the stage for what comes next.

 

<!--- Set defaults --->
<cfcookie name="TICKET" value="0" expires="-1">
<cfset Message="">
<cfif not isDefined("form.UserName")>
  <cfset Username="">
  <cfset Password="">
</cfif>

<!--- Act on login --->
<cfif isDefined("form.Try")>
  <cfif (form.Username contains "f")
  and (form.Password contains "f")>
    <!--- Issue pass --->
    <cfcookie name="TICKET" value="1_a">
  <cfelse>
    Incorrect login
  </cfif>
  - THIS EXAMPLE PRODUCES BROKEN LINKS
  <cfinclude template="../index.cfm">
  <cfabort>
</cfif>

<!--- Accept login --->
<p>
<form name="login" method="post">
<strong>Please log in . . . </strong><br>
[Good logins have an "f" in both the username and password.]<br>
Username <input type="text" name="Username"
size="25" maxlength="25"
value=<cfoutput>"#Username#"</cfoutput>><br>
Password <input type="password" name="Password"
value=<cfoutput>"#Password#"</cfoutput>>  <input
type="submit" name="Try" value="Go">
</form>

Now browse homenot (the default: index.cfm), click the image, enter a good login, and click "Go". Very briefly, what will happen? If login succeeds, the original page will be included, and the rest of the poetic statement will be displayed. Because the page is included instead of moved to by means of the cflocation tag, a cookie can be written when the login "succeeds". This is a good thing.

However, because the page was included from a parent directory, its references relative to the rest of the site are all wrong. The image is not present. If you check the missing image's properties - right click to get a menu - you'll see that it's looking for an image in the login directory. Right-click on the text, and you'll see that this page is effectively homenot/login/mystery.cfm, not the included page. If the included page had links leading to points within the home directory (or above it), all of them would be wrong as well. If you click on the placeholder for the image, you'll get a "not found" error.

The lesson thus far is that when you include a page, the point of view belongs to the calling page, and all the links and image locations of the included page are interpreted from the point of view of the calling page.

So, what's a designer to do? Consider sibling directories.

Sibling Directories

Copy the homenot directory to a sibling directory called home. Move the login directory from within homenot to become a sibling directory called site so that you have three directories at the same level now: homenot (which you won't use again), home, and site. Change the code in home/index.cfm to look like this; the only changes are to the link, the image location, and the cookie test (so the demonstrations won't interfere with each other).

 

<h2>Welcome to my world</h2>
<a href="../site/mystery.cfm">
<img src="../home/earth.gif" border="0"></a>
Life is a mystery
<cfif isDefined("cookie.TICKET")>
  <cfif cookie.TICKET contains "_b">
    and love holds the key
  </cfif>
</cfif>

Now change the code in site/mystery.cfm to look like this; the only changes are the included page location, the cookie value (to keep the demonstrations separate), and removal of the "BROKEN" message:

 

<!--- Set defaults --->
<cfcookie name="TICKET" value="0" expires="-1">
<cfset Message="">
<cfif not isDefined("form.UserName")>
  <cfset Username="">
  <cfset Password="">
</cfif>

<!--- Act on login --->
<cfif isDefined("form.Try")>
  <cfif (form.Username contains "f")
  and (form.Password contains "f")>
    <!--- Issue pass --->
    <cfcookie name="TICKET" value="1_b">
  <cfelse>
    Incorrect login
  </cfif>
  <cfinclude template="../home/index.cfm">
  <cfabort>
</cfif>

<!--- Accept login --->
<p>
<form name="login" method="post">
<strong>Please log in . . . </strong><br>
[Good logins have an "f" in both the username and password.]<br>
Username <input type="text" name="Username"
size="25" maxlength="25"
value=<cfoutput>"#Username#"</cfoutput>><br>
Password <input type="password" name="Password"
value=<cfoutput>"#Password#"</cfoutput>>  <input
type="submit" name="Try" value="Go">
</form>

Browse home (the default: index.cfm), click the image, enter a good login, and click "Go". What happens this time if login succeeds? Because the page was included from a sibling directory, its references relative to the rest of the site are correct. The image is present. If the included page had links leading to points within this directory or other sibling directories, all of them would be right as well.

Finish

You've just seen a simple technique at work: don't nest your directories. Set up multiple directories as siblings all on the same level relative to a common parent that contains almost nothing. Two years ago, I published this as a design approach. This example shows the power of the technique.

The only remaining issue is getting the browser out of the parent directory as soon as possible so this technique can be put into effect. One answer is to have the parent redirect the browser to a "main" directory before displaying anything useful. Here's an example to be placed above the directories you've created thus far. Call it index.cfm.

 

<!--- Include your own code "here" before cflocation
to log the previous URL, count hits, etc. --->

<!--- Now go to the desired page --->

When you browse this top-level index.cfm, it will execute code you give it and then will bring you to home/index.cfm. If you want this page to show off ("splash") or want it to set a cookie, replace the cflocation tag with a link or simple form.

=Marty=

Photo of Nathan Stanford
Nathan Stanford
LinkedIn

R. Marty Ladner's
Site