Well it's April already! Can you believe it? If you're like me, you've got a full-blown case of "spring fever" and it's hard to concentrate on CF code. But spring is also a time for new projects and pressing deadlines.
Don't worry. We're here to help. Just send us your questions and we'll try to get you going in the right direction. Here are some of the questions that came in recently.
Q:I have some questions about server variables. (1) Is there a limit to the number of variables you can create in the server scope? (2) When do they expire? (3) Are they "global" (i.e., available to all sites on the server)?
A:Here are your answers: (1) No, there's no limit to the number of variables you can set in the server scope (or any scope, for that matter). The only limit would be the amount of available memory on your server. (2) Server variables never expire (unlike application and session variables). Once created, they stay in the server's memory until CF is shut down. (3) Yes, that's the purpose of the server variable scope. Once created, these variables are immediately available to any CF page running on that server, regardless of the application.
A word of caution about using the scope: because server variables use a shared memory space, be sure to use CFLOCK around any read or write to the server scope!
Q:I have an application that checks to see if a user has logged in (checks for a Session.LoggedIn variable). How can I redirect a user to the page that was requested first or the page that was up when he or she got logged out because of inactivity, rather than always sending him or her to the home page after logging in?
A:Great question! As with anything in CF, there are probably several ways to accomplish this, but here's how I'd do it. First we need to know what page was requested (whether it's due to an initial request for a page in the site or a user refreshing a page after being away for a while is irrelevant; it's the same process!) just prior to being kicked out to the login page. You can do this by checking the value of CGI.Script_Name. This variable is always available to CF with each page request and it tells you the name of the page the user requested. Next you need to pass the value of that variable over to your login page. Then have your login script direct the user back to that page after successfully logging in.
Listing 1 shows how to capture the value of CGI.Script_Name and pass it over to your login page using a URL variable. You would place this code in your Application.cfm page. Listing 2 shows how to embed the URL.Page-Requested variable in your login form, and Listing 3 shows how to do the redirect after a successful login.
Q:I'm just starting out and have no experience with SQL. Is there a book/guide you can recommend to help me get started with basic SQL?
A:Yes, I'd recommend Sams Teach Yourself SQL in 10 Minutes by Ben Forta. This is a great place to start, and you can read Emily Kim's review in CFDJ (Vol. 2, issue 2).
Q:Do you have a Web site that has an archive of the "AskCFDJ" questions and answers?
A: Yes, you can visit www.NetsiteDynamics.com/AskCFDJ.
Q: Should I use the IIF() function instead of a <CFIF><CFELSE></CFIF> block?
A:NO! You should avoid the IIF() function like the plague! Although this function is very familiar to those of you with traditional programming backgrounds, and it takes fewer keystrokes to code, you should always try to use the <CFIF><CFELSE></CFIF> tags because they execute much faster. Copy the code in Listing 4 into your copy of CF Studio and browse it. You'll see that the CFIF code is up to four times faster!
Please send your questions about ColdFusion (CFML, CF Server or ColdFusion Studio) to Ask CFDJ@sys-con.com.