Putting It All Together ColdFusion Help

This section will demonstrate all the application framework features that you have learned about in this chapter. A full application will be described in detail so that you can see how each of these features is supposed to be employed practically. This application is built on the same basic templates that Y?U originally created, “Creating Your First ColdFusion Template,” Chapter 2, “Passing Data Between ColdFusion Templates,” and Chapter 3, “Getting Data from a Database.” It is a quiz application. The features that have been built into this application by using the application framework are:
• A login procedure that allows entry to authorized users for a single session. After the session has timed out; &sers must log in again.
• ,Global settings using application variables (font face.background color, etc.)
• Storage of query results in application and session variables so that connections to the database are minimized, therefore increasing performance.
• Locking of persistent scope variables to ensure proper read/write access.
• Use of the standard footer template, OnRequestEnd. cfm, to calculate total scores. for the current user and all users.
• Use of session, application, server, and client variables.

If you would like to use this quiz application, you need to do the following:

1. Move the following templates from the chapter17 directory on the accompanying CD to the sybex folder in your W~b server root directory:
• Applitation.cfm
• login. cfm
• login2.cfm
• home.cfm
• quiz_results.cfm
• OnRequestEnd.cfm

2. Move the database qui z. mdbfrom the chapter17 directory on the accompanying CD to the sybex folder in your Web server root directory.
3. ·if you have not already done so, create a data source for the qui zmdbfile in the Cold Fusion Administrator. Call this data source quiz. (If.you need assistance creating a data source,)
4. You mC!y need to stop and start the ColdFusion services in order for the datasource changes to take effect.
5. When your ColdFusion services and Web server are running, open your browser and go to the following URL. cfm.
6. Make sure that cookies are enabled so that the application will work properly.
7. Use the usemame kristin and the password motlagh. The usemames and passwords. are stored in the quiz ,mdbdatabase.

Application. cfm

The Appcation. cfm template is always processed at the beginning of any template in the application structure. It is the ideal place to set persistent scope variables and implement a user verification scheme. First look at the template provided in Listing 17.1, and then read the description of its interactions. The comments provided within the template are also useful for understanding the code.

In this Application. cfm template, the following interactions are created:
1. The use of all persistent variable scopes are enabled,
2. Code to delete session and application structures, as well as client variables and cookies, is provided in a commented tag. This is useful only for testing purposes during the development process. If you remove the comments from around this block of code, you can clear out the variable scopes and start fresh.Iust remember that unless this code is commented out, the application will not work properly.
3. The only server variable that is set in this application is server. season. It is accessible by every page request to the current server. It is used here as an example of creating a server variable. Ideally, if you were to set the season, you would do it dynamically based on the time of year and location in the world.
4. Standard application variables are set for font faces, background color, administrator e-mail, etc. These variables will be used many times on most pages in the application. If they ever had to be changed, you would need to modify only the Application, cfm template as opposed to every occurrence of where these variables are used.
5. The final block of code is used to determine whether a user has logged in and begun a session. If they had logged in, then sessi on. started would be setand this block of code would be skipped. If they hadn’t logged in, then they would be redirected to a login page and all other processing would be aborted. The redirection does not occur if they are already in the login process, because that would result in the user never properly being verified. They would always be redirected to the login page and log;  efm would never process. The verification occurs at log;. efm.

 If the user has logged in and sass ion. started has been set, then processing of the Login,cfm.

The user is sent to this template if they have not logged in and session. started does not exist. They are prompted to log in and begin a nev v· session. Look at Listing 17.2 and then read the explanation of the interactions in this template.calling template occurs as usual.


The user is sent to this template if they have not logged in and ses s ion. started does not exist. They are prompted to log in and begin a session. Look at Listing 17.2 and then read the explanation of the interactions in this template.

First, all the application variables that will be used in this template are set to non-persistent variables. Because you need to use (FLOCKaround any read or write access of an application variable, it is more efficient to read these variables only once they are in the template, thereby using only one CFLOCK tag. These variables, once reset to nonpersistent variables, can be accessed throughout the page without adding additional CFLOCK tags.
Second, the login form is created. You will notice that if cl i ent . user_name exists, then the user is welcomed back and prompted to start a new session. Although it appears that this client variable may not be very useful, you could use this information to track how many unique visitors there are to your site, even though you still want .them to log in each time.

Finally, the Usemame and Password fields are displayed so that the user can log in. If you are testing this application, you can use the user name kristin and the password .
Now these fields will be sent to log2 cfm to be verified against a database.

Remember that for each of these templates, App 1cation. cfm is processed first. Because at this point the user has not been verified and session. started has not been initialized, the Application. cfm template would normally throw the user back to login2. cfm. But a statement was added to the Application. cfm file that prevented ‘this from happening if the calling template was either log; n. cfm or login2 . cfm. You can refer back to Listing 17.1 for a more complete description.

First, the application variables are again set to non-persistent variables so that a minimum of CFCLOCK tags are used. Second, the form fields user _name and password that . are passed to this template are checked against a database containing valid usernames and passwords.
If the query returns no results, this means that no matching user_name and password combination was found and the user must try to log in again. The login2. cfm template is then included in this section of code. the login screen if the user was not verified.

But, if the user is verified, then ses s t on. started is initialized and the client variable user_name is set. Then the user is redirected using a CFlOCATION tag to the home. cfm template and they can begin using the quiz application.

Posted on November 16, 2015 in Implementing the coldFusion Web Application Framework

Share the Story

Back to Top
Share This