Coldfusion Scopes

So today I got a fun email. One of our web service tools stopped working suddenly and was returning the error message "Variable FORM not defined". This struck me as odd, because as far as I knew, the FORM scope should always be in existence.

After doing a little reading I learned this isn't always the case. It turns out that when rendering a WSDL address, the FORM scope never exists. This means that anytime you use either an optional form element or directly reference form, it MUST be explicitly defined somewhere in your page. This should also be taken into account if you are still using application.cfm as, of course, this file will be part of your WSDL files source code.

As a rule of thumb, one should never expect a specific scope to exist in application.cfm/application.cfc, since it can be used across all page rendering views. Additionally, different scopes can be available at different points in the rendering of a file. An obvious example being during OnApplicationStart. At this point in the page rendering, SESSION isn't yet available, however APPLICATION is.

Remember: Always scope your variables, not just because of the above information, but also because it can become very cumbersome to attempt to figure out the exact scope of a variable once it's deep in code.

Connecting to HTTPS urls via CFHTTP when the imported certificate fails

Sometimes I find problems that a solution is everywhere for, however when I attempt to implement it, I find the fix still doesn't work. This is what I encountered when attempting to connect to a web service provider the other day. Here was my situation, I was connecting to a web service that happened to have its data on an HTTPS server.

For the example we'll call the server "HTTPS://TESTSERVER.EXAMPLE.COM/FOO/BAR". When I tried to connect I was met with the following error:

Using the steps on the following site, I was able to import the server certificate and get it inserted into our CACERTS file.

http://blog.flexuous.com/2008/04/22/install-server-or-ca-certificate-in-coldfusion-or-java-virtual-machine/

I still kept getting the "peer not authenticated" issue. At first I thought it could be that I was updating the wrong CACERTS file or maybe I needed to update another section. That led me nowhere however. Finally, I had to take a step back and review my assumptions. I reviewed the certificate and all it's attributes. The biggest one that stuck out was the URL. Remembering that Java is indeed case sensitive I decided to update the URL have the domain be lower case: https://testserver.example.com/FOO/BAR

I gave the URL a try and lo and behold the process connected! I was happy to see this result, but I wanted to know exactly which part of the URL was causing the problem. I changed the subdomain testserver to upper case. The URL still worked not surprisingly, since the certificate is related to the domain specifically. What did surprise me was when I changed the domain to upper case. The connection still worked. Changing the only thing left to change, I updated the "https" to upper case. This duplicated the error.

My takeaway from this experience: If you're going to be creating CFHTTP calls, make sure your HTTPS text is lower case.

-Jonny

Clean layouts and spacing

Here is a fun challenge for those of you who have to deal with specific sized divs that contains dynamic text. What do you do with a div that you cannot or do not want to resize dynamically. Today, I have two solutions for you.

The first and simplest technique would be to use CSS to hide overflow content.

Lets take a simple piece of code.

view plain print about
1<div style="width:100px;border-top:1px solid #000000;">&nbsp;</div>
2<div style="border:1px solid #000000;width:100px;">
3This_is_an_unbroken_line_of_text!!!!!!!!
4</div>

[More]

JPG files which won't write

Here was a problem that occurred for me the other day that you all may have encountered. When I was attempting to upload certain image files, Coldfusion would throw an error!

"An exception occurred while trying to write the image. Ensure that the destination directory exists and that Coldfusion has permission to write to the given path or file. cause : coldfusion.image.ImageWriter$ImageWritingException: An exception occurred while trying to write the image."

[More]

Forcing indexes to run in SQL

Did you know you can actually force an index to run if you'd like? SQL table hints are the key here. Using these hints you can indicate that you'd like the query to give preference to once index or another. This can sometimes be beneficial if you'd think that one index may be better than one the SQL query tools automatically select.

[More]

Cfmail in cfscript

Cfmail in cfscript Here's a convenient little function I use for some of my more processing oriented pages. I love building content in cfscript however I find it really annoying having to close my cfscript tags to mail content. In general I don't like closing cfscript tags for anything if at all possible.

[More]

Using CFHeader to hide file paths

Recently, while working on a project, I was tasked with hiding the source path of downloadable content.  I knew that if I passed the filepath anywhere on the server it wouldn't be secure.  A great tag to use if you don't want to share file paths, or if you want to store files in somewhere other than the webroot folders is the ""CFHeader" tag.  CFHeader sets the content header and in this case can be used to change how the file is grabbed from a server.  One can even make images download by default instead of loading in the browser.

[More]

Cleanliness in Code: Saving memory on while running a page

There are some really quick and easy ways to save memory in Coldfusion which take very little effort.  Here are some of them:

Empty your variables after using them.

It may seem silly to do this since CF does a rather good job of cleaning up memory after it uses a variable, however you'd be surprised how much memory really gets used by variables. 

[More]

An odd error: Content is not allowed in prolog.

A few weeks ago I encountered this particularly odd error while I was working on a video import process.  The process read in an XML feed, parsed it, and set the data into a database.  The process in question had been working fine for month until this point, and I couldn't for the life of me figure it out.  I knew the error was occuring on the XmlParse function line 1790:

view plain print about
11789 <cfhttp resolveurl="No" method="GET" url="#qFeeds.feed_address#"></cfhttp>
21790 <cfset x="XMLParse(cfhttp.FileContent)"> </cfset>

I also knew that when I dumped the CFHTTP content, the data looked correct.

It turns out the reason I wasn't seeing anything wrong was this was a whitespace problem. Surrounding the variable cfhttp.FileContent with the trim function took care of this for me. If you get this error and don't see any xml out of place, see if this works for you.

Another Simple Tip: Selecting the last inserted ID

I know that when I'm using SQL I find myself frequently having to get my ID that I just inserted into a table, so today I'm going to sharesome of the tools I use to get the last ID I inserted into a database.  Sometimes when beginning to learn SQL you may feel the need to create a transaction and do a select for the greatest value of your primary key field to get the identity.  Although this sounds great, firstly, it requires a transaction, and secondly, it doesn't take into account non-int or non-incremented keys. 

[More]

More Entries