ASK THE CF DOCTOR
By Douglas Smith and Michael Smith
The
CF Doctor was at the CF 2001 Odyssey conference answering attendees’ ColdFusion
programming questions. We look at some popular questions and answers here.
Question:
A lot of users want to know how they can get to files
outside of the physical server that CF Server is located on. This includes ODBC connections to file
based databases, such as Access97.
Answer:
This is a common problem that first time users of CF run into on
NT.
CF Server runs as “system”, which means is has permission to do
everything on the local box, but does not have any network permissions to go
out over the network and get files from other machines. If you need to do this, take the following
steps in NT:
1)
Open the Services option from the control
panel.
2)
Double click on the ColdFusion Executive
service
3)
Change the “Log On As” option from “system” to
a real user, for example “Administrator” or make a new user just for this
ColdFusion service. Whatever
permissions this user has on the network, Coldfusion will then have.
Question:
From
Robert Irving, Biophoto
Verity Search has ceased functioning on the server and
any new collections can not be created.
Answer:
This is a common problem when upgrading from CF Server 3.5 to 4.01. The real solution at this time is to upgrade
to 4.5, with the 4.5.1 patch.
However, you can also read about this problem in the Allaire's Knowledge base,
Article 9069. The solution suggested is to rename the following file:
C:\cfusion\verity\common\style\custom\Style.vgw
To
C:\cfusion\verity\common\style\custom\style.vgw
Then delete the indexes and recreate.
http://www.allaire.com/Handlers/index.cfm?ID=9069
Question:
From
Syed Hasan, MCI WorldCom
How can we enhance, and speed up dynamic web pages
generated by ColdFusion.
Answer:
This is something everyone wants to know about, and entire
articles have been written on this subject!
But let’s try and be brief…
First, determine if the problem is with ColdFusion or with your
HTML. Turn on debugging in the CF
admin, and make sure you have “Show Processing Time” (with Detail View in 4.5)
turned on. Then check the time CF takes
to generate the page. If this is a large
number, then you need to spend some time optimizing your CF code. If the CF number is low, but the page still
takes a long time to display on the browser, then the problem is probably with
your HTML.
Common HTML Speed-Up Suggestions
- Break
up long tables! Page your data
display (Page 1 of 10 for example.), or close your tables every 100
records (or so) and start a new table.
- If
you must display long tables, take out tabs and white spaces before all
<tr> and <td> tags.
You might loose your nicely indented code, but it really does help!
- Avoid
deeply nested tables (say, more than 5 or 6 tables deep).
- If
you have the option, use IE. IE 4
handles nested tables (and long, complicated tables in general) much
better than Netscape 4.
- Put
sections that are CF code intensive between <CFSETTING
enablecfoutputonly=”YES”> and <CFSETTING enablecfoutputonly=”NO”>
tags to get rid of unwanted white space that takes time for your browser
to render.
Common CF Speed-Up Suggestions
- Use
CF template cache! Set your
template cache size in the CF admin to a larger number.
- Use
<CFQUERY cachedwithin or cachedafter option> to cache queries that
are likely to be similar, or do you own caching for global queries (like
lists of states or users, etc), run the query once on start up, and save
the query results to an application variable, and use that in <cfoutput
QUERY=”#Application.MySavedQuery#”>
3. Make the database do more of the work! Most slow web
sites are due to inefficient use of the database behind the web site, so learn
more sophisticated SQL, and use stored procedures, grouping and calculated
fields in summary queries, instead of having CF do the calculations in a
loop. The database will ALWAYS be able
to calculate data faster that ColdFusion.
4. See the question/answer below on dynamically generating
fast, static HTML in place of CF pages.
Question:
From
Larry C. Lyons, ebstor.com
I want to generate a series of static HTML files
dynamically, based on some CF templates.
How can I have CFFILE read a template, insert CF variables in the
appropriate places, and then save the output results to a static HTML file?
Answer:
A lot of people realize how fast HTML files are without CF! However, you can use CF to dynamically
“write” static HTML files themselves!
For example, if you wanted to make a static THTMHTMHTMLhtmHTML page for every
record in your publications table, you might do something like the following:
1.
Make a template with CF variables that would
display your fields from your publication table.
2.
Run a query to get your publication
table. Let's say there is an ID field
for each pub called "Book_ID"
3.
Read the
template into a CF variable:
<CFFILE action="READ" file="Pub_Template.cfm"
variable="Pub_Template">
4.
Loop over the query, writing a file for each
record in the DB.
<CFLOOP query="#MyPublications#">
<CFSET MyPub =
Evaluate("Pub_Template")>
<CFSET Pub_FileName =
"pub_#book_id#.html">
<CFFILE action="WRITE"
file="#Pub_FileName#" output="#MyPub#" >
</cfloop>
5.
You might want to first delete all files in
the directory you are writing to, or check for existing files before writing,
so you don’t get errors, or delete files unexpectedly.
6.
You could run this code on a schedule, and
“re-generate” your web site once a week or once a day, keeping it up to date
and very, very fast. Use the
<CFSCHEDULE> or the NT scheduler (AT command).
Question:
From
Taiyebali Zakir, Soza & Company
How can I encrypt and save passwords in my SQL Server table?
Answer:
Use the Encrypt() function that comes with CF, or download one
of the many encryption tags from the Developer’s Exchange section of the
Allaire web site.
<CFSET
encrypted_pass=encrypt("#form.password#","mycatisadingbat")>
By the way, you don’t have to decrypt the password from the DB
to compare it! One the encrypted
password is saved, and the user is registered, the next time the user logs on,
simply encrypt the value from the form and compare it to the encrypted string
from the database. If you don’t provide
a decryption routine, then this allows you to have “one way” encryption where
the user’s password cannot be discovered, even by the system administrator.
Question:
From
Jimmy Jose, SETA Corporation
How can I attach multiple documents in one email?
Answer:
CF Server 4 comes with an <CFMAILPARAM> child tag, just
for this purpose:
<CFMAIL TO="recipient" SUBJECT="msg_subject" FROM="sender">
<CFMAILPARAM FILE="file-name1" >
<CFMAILPARAM FILE="file-name2" >
Body of email text goes here….
</CFMAIL>
Question:
From
Kathy Hillstrom, Lockheed Martin
Using CF 4.5.1, if I restrict the CFREGISTRY tag (in the
CF Administrator), then I can’t use the ColdFusion Scheduler. Is running the NT scheduler (AT) the only
option?
Answer:
This stumped the CF Doctor, even though methinks the
answer is “Yes, use the NT scheduler.”
Email me with your opinions about this problem, and any other feed back, to:
[email protected]
The
CF Doctor is Douglas Smith who is the web/database architect for TeraTech.
Michael Smith is president of TeraTech http://www.teratech.com/
a 12-year-old Rockville, Maryland based consulting company that specializes in
ColdFusion, Database and Visual Basic development. TeraTech runs the MDCFUG and recently organized the two-day,
Washington, DC-based CF2001 conference that attracted more than 150
participants. You can reach Michael at [email protected]
or 301-881-1440.