Pages

Showing posts with label ColdFusion. Show all posts
Showing posts with label ColdFusion. Show all posts

ColdFusion - VariableTypes

0 comments

"Variables"

ColdFusion Variable Types:

  • A variable is a named location in memory that holds a value. The "type" of variable refers to the type of data stored at that memory location. Some languages strongly resist converting numbers to strings (text) values and vice-versa; these languages are referred to as "strongly typed." ColdFusion is a "loosely typed" programming language. 
  • This means that variables are not (generally speaking) strongly associated with one data type or another; for example, a variable assigned a numeric value can be used as if it were a string variable. The length of text variables need not be defined before use, and I've created string variables several megabytes in length. 
  • The basic data types in ColdFusion (not that it's really all that important) are Numeric, String, Date/Time, and Boolean.
  • There are others data types: arrays can be created; SQL queries, once executed, become a type of array; and structures were added in version 4.0. A List is almost another data type is ColdFusion, but it is actually just a string with some sort of delimiter, usually a comma. Special variables cannot be automatically converted to other types.
  • A string can only be converted to a numeric if it looks like a number. "45.3" can be converted; "two" cannot be converted. Any number can be converted to a string. The two basic Boolean values are TRUE and FALSE; however YES and NO work well too. Non-zero numbers are considered TRUE; zero is considered FALSE. String values other than true, false, yes, no, or something easily converted to numeric, will cause an error to be displayed.
  • Variables names are NOT case-sensitive, must start with a letter, and can include letters, numbers, and underscores ("_"). "Customer_Name" is valid; "2Customer" and "Customer Name" are not valid variable names. The value of a variable is displayed by expressing the variable in pound signs, within a CFOUTPUT block:
        <CFOUTPUT>Welcome to our online system,
        #Customer_Name#!</CFOUTPUT>
  • All variable names that contain a single value exist in "variable scopes." (More about that in the next section.)

SQL Data Types:

  • SQL (as implemented by ODBC) has three data types: string, numeric, and date/time (or timestamp.) SQL queries are sent to SQL-based databases via the <CFQUERY> tag.

SQL Strings:

  • SQL strings begin and end with a single quote ('). If you want to embed a single quote in a query, you need to type two single quotes. (Programmers generically called this 'escaping' a reserved character.) Therefore, to express the value:
     I don't get this.
          as an SQL string, you would type:
     'I don''t get this.'
  • Within a CFQUERY, ColdFusion will replace single quotes in #variables# with double quotes automagically; however, ColdFusion will not do single quote escaping for functions. Therefore, any data that may contain single quotes should be manipulated outside of the CFQUERY statement.

Wrong:
<CFQUERY...>
    SELECT * FROM Users
    WHERE UserName = '#ucase(User_Name)#'
</CFQUERY>
  
Right:
<CFSET UserName = ucase(UserName)>
<CFQUERY...>
    SELECT * FROM Users
    WHERE UserName = '#User_Name#'
</CFQUERY>

SQL Numbers:

  • Numbers are provided to SQL without any special consideration. However, ColdFusion programmers must be sure to handle numeric values passed into queries! Otherwise, the supposedly numeric value could be 'hijacked' by a malicious user and used to run other SQL commands. Say a link is created that passes a numeric row identifier "OrderID." OrderID is the primary key for the table "Orders" and is numeric. Although it's tempting to write a query to get the order from the database like this:
SELECT *
FROM Orders
WHERE OrderID = #OrderID#

  • Someone could easily replace the numeric OrderID (e.g. "5") an the URL line (or alter the form, etc.) with a value followed by a malicious command, such as "http://myserver/MyPage.cfm?OrderID=5+delete+from+orders". The SQL passed to the database system would then look like this:
SELECT *
FROM Orders
WHERE OrderID = 5 delete from orders
  • Most high-end database systems allow more than one SQL statement per query. Without very conservative database security settings, the above command would delete all orders from the system. You don't want to get that phone call. All numeric values passed by the user should be wrapped by the ColdFusion val() function, which returns the numeric value of the parameter supplied to it, or zero (0) if the value is not numeric. 
  • Therefore, val("5") is 5, but val("5 delete from orders") is 0. The code would be properly rewritten like this:
 
SELECT *
FROM Orders
WHERE OrderID = #val(OrderID)#

SQL Date and Date/Time values:

  • ODBC was designed to provide one consistent application interface to many different DataBase Management Systems (DBMSes). There is a problem with providing one SQL front end to many DBMS back end systems...
Date/Time values, in ODBC, are provided in one of the following three formats:
  • Timestamp: {ts '1999-11-01 15:14:23'}
  • Date: {d '1999-11-01'}
  • Time: {t '15:14:23.00001'}
For time (and timestamp) values, the seconds and fractional seconds portion are optional. Many SQL systems do not support fractional second timestamps; if you attempt to use a fractional second value with these systems, strange errors may occur.

An example SQL Insert showing all three data types:
 
<CFQUERY Name="InsertCustomer" DataSource="CustomerData">
    INSERT INTO Customers (
        CustomerName
               ,CustomerAge
               ,DateAdded
    ) VALUES (
            'John''s Bakery'
               ,30
               ,{ts '1999-11-01 15:54:00'}
        )
</CFQUERY>

ColdFusion - Introduction

0 comments

What is ColdFusion?

  • ColdFusion is a programming language. Like Java, in the sense that it is nothing by itself; it requires a programmer with vision and skill to make anything useful out of it. Like Visual Basic, in the sense that the language is tailored for a specific purpose, but general enough to accomplish most any task easily within that scope. (Visual Basic is designed to make applications for the Windows environment; ColdFusion is designed to make applications for the Web server environment.)

Databases

  • ColdFusion was originally designed for allowing database contents to be easily displayed in Web pages for any browser, without plugins, by manipulating the HTML output of a Web browser. Since the browser is fed nothing other than standard HTML, no plugins are needed.

More..!

  • ColdFusion also makes gathering, manipulating, and presenting data from other data sources easy, including other Web servers, FTP servers, ASCII data files, POP mail servers--not even COM and CORBA objects are safe :-)
Web application server:

  • ColdFusion is an application server that runs on a web server. It runs on Linux, Solaris, Windows servers and even the personal web server on Windows 98. The ColdFusion Web application server works with the HTTP server to process requests for web pages. 
  • Whenever a Cold Fusion page is requested, the ColdFusion Application Server executes the script or program the page contains. Unlike JavaScript and Java applets, which run on the client, ColdFusion runs on the Web Server itself. Scripts you write in Cold Fusion will run the same way on every browser.
  • ColdFusion server was developed by the Allaire Corporation to be a simple to use yet powerful alternative to Perl and other CGI technologies. 
  • ColdFusion server is a Rapid Application Development system for professional developers who want to create dynamic Web applications and interactive Web sites. It provides a fast way to integrate browser, server and database technology into Web applications. 
  • Developing applications with Cold Fusion does not require coding in a traditional programming language like Perl, C/C++, Visual Basic, Java. You build applications by combining standard HTML with a straightforward server-side markup language, the Cold Fusion Markup Language (CFML). Cold Fusion is a markup language. 
  • It can create and modify variables and has program flow controls. It can perform quite complicated tasks. With over 75 CFML tags and over 250 custom functions any web application can be built. Input for Cold Fusion scripts often comes from a user submitting a form to a Cold Fusion template. 
  • Cold Fusion application pages handle everything from capturing data a user enters to presenting output. Cold Fusion scripts can also get their input from URL strings, CGI variables or databases. Pages with the .cfm extension are processed without the need for CGI-style URLs to specify an executable. 
  • Output from Cold Fusion scripts is typically sent to a user's web browser, put into a database, written to a file or e-mailed. Cold Fusion was designed to build complex, high traffic web sites. ColdFusion server is designed to run on multi-processor systems and allows you to build a dynamic web site that can be run on a cluster of servers.
  • Use ColdFusion server to build and maintain databases, process forms, make parts of your web site secure and gather or publish data. You can use it to build web applications like bulletin boards, pop mail clients, online-calendars and chat rooms. Cold Fusion scripts can be written to track hits, clicks, return visits and other valuable traffic statistics. 
  • A Cold Fusion application could consist of many application pages, depending on the complexity of your application. Each page performs specific functions in the application. Basically, any function you find on the Internet today can be built and maintained with ColdFusion server.
Databases:
  • ColdFusion makes interacting with your database (Sybase, Oracle, MySQL, SQL or Access) simple. Using standard SQL (Structured Query Language) your web pages and web applications can retrieve, store, format and present information dynamically. If you are known with HTML, CFML (Cold Fusion Markup Language) is just one step further. 
  • Reading from and writing to servers is tag based. The <CFFILE> tag takes "arguments" which specify 'action=read/write/copy/delete', 'path=' , and so forth.
  • The CF tag <CFFORM> will automatically build all the JavaScript code to verify required fields before the form submits. Cold Fusion also has tags to embed COM and Java applets and servlets.
ColdFusion Components:   
  • Cold Fusion applications need the following components.

The ColdFusion Application server: 

  • The application server runs as a service under Windows NT. The application server listens for requests from the web server to process Cold Fusion application pages. You can stop or start the service.
The ColdFusion Administrator:
  • You use the Administrator to configure various Cold Fusion Application server options, including: Cold Fusion data sources,debugging output, server settings and directory mapping.
ColdFusion Application pages: 

  • These are the functional parts of a ColdFusion application, including the user interface pages and forms that handle data input and format data output. ColdFusion makes it simple to pass data to a data source and to receive and format data for display.
ODBC Data Sources: 
  • ColdFusion uses ODBC to interact with a database therefore you can use any database that supports ODBC as a data source for your Cold Fusion applications. 
  • A ColdFusion data source can be any ODBC-compliant database, as well as an Excel spreadsheet, a text file, an LDAP directory server and others.