ColdFusion 4 variable

Source: Internet
Author: User
Tags ranges
Variable Creation

Variable Creation
In ColdFusion, a variable is assigned a value parameter, and the use of the variable exists in ColdFusion
It is unthinkable to develop applications without using variables in all aspects of applications. For example
To pass the data in the form to the webpage specified by the Action attribute. This variable is also called dynamic
Variable Creation
Use cfset to create a variable
The simplest way to create a variable is to use the cfset tag. You can use the cfset tag anywhere in the application,
The created variable can be referenced anywhere after the tag in the same file. You only need to specify the variable name and assign
A static value, a parameter, or expression. Its syntax is:
<Cfset variablename = value, parameter, or expression>
Example 1: static Value
<Cfset firstname = "Jack">
In this example, a variable firstname is created and its value is the static value Jack. Note that when the variable value is a string, use
Double quotation marks. Another example is to create a variable usernumber with a value of 26:
<Cfset usernumber = 26>
Example 2: Dynamic Parameters
<Cfset currentuserid = getuserid. user_id>
In this example, getuserid is the query name of a database query operation, and user_id is a field name queried. This
For example, the variable currentuserid is defined, and its value is the value of the user_id field in the database query result. Another example is:
<Cfset userdescription = "# username # is a wonderful person.">
Username is another variable.
Example 3: Expression
You can use cfset to create a variable and assign the value of an expression to it, for example:
<Cfset totalvalue = 2*(4 + 5)>
<Cfset pay = "John's take home pay is" & (TotalValue-1000)>
Note: in ColdFusion, there are no types for variables. That is to say, you do not need to specify the data class of the variable value.
Check the existence of Variables
Before using a variable, you can use the isdefined function to check whether the variable exists. For example
Test whether the variable named order_id exists:
<Cfif not isdefined ("form. order_id)>
<Cflocation url = "previous_page.cfm">
ColdFusion cannot process the file if you try to use an unspecified variable.

Use cfparam to create default Variables

Use cfparam to create default Variables
Another way to create a variable is to use the cfparam label, which first checks whether the variable exists.
If it does not exist, a default value is provided. The syntax of the cfparam label is:
<Cfparam name = "variablename" default = "defaultvalue">
You can use the cfparam tag in either of the following ways:
· Only the name attribute is provided to check whether the required variables exist. If not, ColdFusion
Server stops processing the file. For example, the following statement indicates that the file requires two variables: startrow and
Rowstofetch, processing can continue:
<Cfparam name = "form. startrow">
<Cfparam name = "form. rowstofetch">
If the file containing the two tags is called but no one of the two variables is provided, an error occurs,
The file is stopped.
· Both the name and default attributes are provided to check whether an optional variable exists. If yes,
The processing continues and the value of the variable is not changed. If the variable does not exist, the variable is created and assigned a value
The value provided by the default attribute. For example:
<Cfparam name = "Cookie. searchstring" default = "Temple">
You can use the cfparam label instead of the branch statement to set the default value for the URL and form variables.
Display the usage of variables and well numbers on the webpage
To display a variable on the webpage, you must include the variable in the cfoutput tag. For example:
Your favorite color is # Clint. favoritecolor #.
Note: In the cfoutput label, always include the variable name in. This indicates that the variable name will be
The value is calculated as a dynamic parameter. ColdFusion outputs the value of the variable instead of the variable name itself.
When you want to distinguish whether a string is a variable or text, you need to use a well number to indicate that the position should be determined by the value of the variable.
. For example:
<Cfquery name = "Search" datasource = "company">
Select * from employees where lastname = '# form. lastname #'
In the cfset label, do not abuse the well number. For example, do not use <cfset x = # Cos (0) # +> instead.
<Cfset x = cos (0) + 1>
Because the well number has a special purpose in ColdFusion, two consecutive well numbers are required to represent the well number itself.

Variable naming rules

Variable naming rules
ColdFusion variables and form fields should follow the following naming rules:
· The variable name must start with a letter, followed by any letter, number, or underline.
· The variable name cannot contain spaces or special characters. For example, username_1, username_2, username
Is a legal name, 1 stuset, whataname !, And user-name are both invalid variable names.
· For field names and variable names, descriptive names should be used instead of abbreviations. This makes it easier for others to read
Understand your program and help you remember the purpose of the variable.
· Note: In the same CFM file, the variable name and query name cannot be the same.
· ColdFusion variables are case insensitive.
Variable reference range
You have noticed that in some of the above examples, the variable name has a prefix and will be discussed below.
ColdFusion variables do not have data types, but they have different sources (such as from forms and queries) and
The same scope of reference (such as within the scope of this file and within the scope of the application ). ColdFusion uses prefixes to differentiate
Variables of the same name from different sources or reference ranges. For example, form. State indicates the state of the variable submitted by form,
URL. State indicates the state variable from the URL, which is two different variables. When creating a variable
Variables of different ranges have the same name. You do not need to use the prefix, but use the prefix to make the program clearer and increase
Processing speed.
The following table lists the types of variable prefixes in ColdFusion. Details about these types will be discussed later:
Variable prefix
Query Variables
Queryname. variablename
Local variable
Variables. variablename
URL parameter variable
URL. variablename
Form field variable
Form. variablename
Client variable
Client. variablename
Server Variables
Server. variablename
Session variable
Session. variablename
Application variables
Application. variablename
Cookie. variablename
CGI Environment Variable
CGI. variablename
When you do not specify a variable prefix, ColdFusion searches for the variable in the following order:
Local variables created with cfset and cfquery
CGI variable
File variable
URL variable
Form variable
Cookie variable
Client variable
Note: ColdFusion does not automatically search for Application variables and session variables. The two variables are defined and used.
Must be prefixed.

Variable type

Variable type
The following describes ColdFusion variables one by one.
Local variable
Local variables are the default types of variables created using the cfset label and cfparam label. This variable can only be created in
It is referenced in files or files that contain the file.
Query Variables
Once a query is executed, you can use the query variables to access the query results. The prefix of the query variable is query.
The variable name is the field name in the table. For example, if you run a query named lookupuser
If one of the fields is a field named ID, you can use the lookupuser. ID variable to access the ID word in the query result.
Segment value.
URL variable
URL variables are appended to the URL in the form of variablename = value and passed to the webpage indicated by the URL.
Use URL. variablename to reference the variable.
Form variable
The most common method for passing parameters between webpages is to use form variables. When the customer enters data in the form field and submits the data,
The input data is transmitted to the webpage indicated by the Action attribute in the form of a form variable.
Form. variablename references this variable. variablename is the name of the form field.
Cookie variable
Cookie is a mechanism by which server applications (such as ColdFusion) store information in browsers. Storage
The cookie in the browser can be returned to the server application. Using cookies, applications can create
Variables related to a specific browser. For example, you can create a cookie for the background color to make your site
The dots have different colors in different customers' browsers.
Cookie is related to a specific site. When a browser accesses a specific site
To the server.
You can use SSL (Secure Sockets
Layer. Cookies will exist until they expire or are deleted. Current mainstream regions
The browser supports cookies.
Create cookie
The tag cfcookie is used to create a cookie, for example:
<Cfcookie name = "user_id" value = "2344"
Expires = "100" type = "codeph" text = "codeph">
In this example, the variable cookie. user_id is created with a value of 2344 and will expire after 100 days.
After a cookie is created, all ColdFusion web pages on the same site can reference the cookie. This means that you
You can use cookies to pass parameters between webpages. Note: cookies are not suitable for storing security information, such as passwords and emails.
Card number.
Note: If the cflocation tag is executed after the cfcookie tag in the cookie creation file
The cookie created by the cfcookie tag will be lost.
Use cookies on webpages
Once a cookie is stored in the client browser, the cookie variable is
It is automatically sent to the web server. Like other types of variables, you can use the prefix to reference the value of the cookie variable,
For example:
<Cfoutput> # cookie. user_id #
Delete cookie
To delete a cookie, you can use the cfcookie label to set the expires attribute to "now". For example:
<Cfcookie name = "user_id" value = "# user_id #"
Expires = "now">
When the browser is closed, the cookie will be deleted.
Client variable
Client variables are variables related to specific customers. When the customer moves between different files of an application (
"Mobile" refers to the customer's link from one file to another), the client variable can be used to maintain the customer's
Status to identify a specific customer.
For an application that has started customer status management, ColdFusion is the identifier of each application file that requests this application
The browser creates a customer record, which is identified by a unique token. the token is stored in
In a cookie. With the customer record, you can define client variables in the customer record.
Any file of the entire application is referenced. When the customer requests a file, Coldfusion provides
So that all client variables can be used.
To create a client variable, follow these steps:
In the cfapplication tag of the application. cfm file, set clientmanagement = "yes" to enable
Customer status management. (This article introduces the application. cfm file)
In the cfapplication tag, set the clientstorage attribute to select the storage location of the client variable.
The default location is set in ColdFusion Administrator. It can be the Registry, an existing data source, or
Use cfset or cfparam to create the variable. For example:
<Cfset client. favoritecolor = "conflower blue">
Standard client variable
Coldfusion provides several standard client variables that help record the number of times customers visit your site and
The last access time and other information. For example, the following example shows the last time the customer visited your site:
Welcome back to the Web supershop, your last visit
Was on # dateformat (client. lastvisit )#.
Standard client variables have read-only attributes. You can access them, but cannot set their values in the program. These
Variables include:
· Urltoken
· Hitcount
· Timecreated
· Lastvisit
Customer status management without using cookies
In ColdFusion, customer status management is designed to use cookies, a standard method for identifying a customer.
You can also manage the customer status without using cookies. To do this, you must use the hidden form word
The customer ID (CFID) and the customer Security token (CFTOKEN) are transmitted between webpages.
High programming skills.
Storage of client Variables
The default storage location of client variables is the system registry. You can use ColdFusion Administrator to change the storage location.
By default, the client variable is stored in the SQL database or cookie. Used in the application. cfm File
The clientstorage attribute of the cfapplication label can be used to set the storage location of the client variable.
Build the default settings of ColdFusion Administrator. For example:
<Cfapplication name = "myapplication"
Clientmanagement = "yes"
Clientstorage = "mydatasource">
If the clientstorage attribute is not set, ColdFusion uses the default setting. Note: The storage of client Variables
The storage mechanism is exclusive. When a storage type is used, the client variables of other storage types are not accessible.
When using cookies to store client variables, consider the following constraints:
· Netscape Navigator allows only 20 cookies for a specific host. Use ColdFusion
Two cookies are used for CFID and CFTOKEN, and a cookie named cfglobals is used to store the global number of customers.
Such as hitcount, timecreated, and lastvisit.
· Netscape Navigator sets the limit that each cookie cannot exceed 4 K bytes. That is to say, you are not
Store too much data in cookies.
· If the client browser prohibits the use of cookies, the client variable will not work.
Obtain the client Variable list.
Use the getclientvariableslist () function to obtain the list of client variables.
The name of the client variable within the application range determined by the cfapplication. Standard
The client variable is not in the returned list.
Delete client Variables
Use the deleteclientvariable () function to delete client variables. For example:
Isdeletesuccessful = deleteclientvariable ("myclientvariable")>
This function can only delete client variables within the application range determined by the cfapplication.
Similarly, ColdFusion
Administrator settings, you can delete the client variable after a specific number of days
The default value of the variable in the table is 90 days. For the variable stored in the database, the default value is 10 days. Note that you do not
Can delete standard client variables provided by the system.
Application and session Variables
Application variables and session variables are all variables within the application scope.
Can be accessed. You must add a prefix before the variable name to access these variables. For example,
"Session. myvariable", "application. myvariable ". Because they can be used in the entire application
Therefore, they can be used to transmit data between webpages.
Allow Application variables and session Variables
Like client variables, you can use the cfapplication label to allow application variables and session variables.
(For more information about the syntax, see ColdFusion documentation CFML language reference)
Different from client variables, Application variables and session variables are always stored in the server's memory,
This makes them faster to access. In addition, you can use ColdFusion Administrator or
In cfapplication, set the survival time of these two variables. You can also completely disable Application variables and
Session variable.
Session variable
Like the client variable, the session variable is related to a specific customer. It also requires a customer ID and starts
The end is associated with the customer ID. When the customer requests a ColdFusion application file for the first time
Sessions, the session has a certain survival time, if the customer did not send the request to the server within a limited time
The session ends.
If two clients connect to the same application on the server, this is two independent sessions, each of which
They all have their own session variables, which may have different values, so customers cannot access others' session variables.
Session variables are suitable for storing global information related to specific customers. When a customer requests a webpage for the first time
Information is initialized in the session variable. This information can still be directly used when the customer accesses other webpages.
To allow session variables, you can set them in the cfapplication tag of the application. cfm file.
Session variable lifetime
The session variable has a specific lifetime, which defines a "session ". When you are in
When a session variable is accessed during its lifetime, the variable can be accessed. If the lifetime is exceeded, the variable
Does not exist.
The default expiration time of the session variable is 20 minutes, that is, if the browser does not contact the server within 20 minutes,
The session will be ended. You can use ColdFusion Administrator to change this default setting.
You can also use the sessiontimeout attribute of the cfapplication label to set the expiration time of the session variable,
This will overwrite the default settings of administrator.
Application variable
Application variables are also variables that can be accessed throughout the application. Unlike session variables,
The application variable is not specific to a specific customer and does not need to be associated with a specific customer ID.
All customers can access the same application variable. This variable is suitable for storing application-level non-unique
Customer-related data that is used by all customers.
Here it is necessary to explain what is "application ". In ColdFusion
Set the name attribute in the cfapplication tag of the file to specify an application name for the application.
Files with the same application name belong to the same application, while all files of the same application share all
Application variables. These variables are the same for all customers. (About the application. cfm file and
The definition of "application", which will be described later)
Lifetime of the application variable
The application also has a certain lifetime. When the first visitor requests a file in the application,
ColdFusion initializes the application. If no client requests the application file within the specified time,
The application is terminated and the application variable does not exist.
The default expiration time of the Application variable is 2 days. You can use ColdFusion Administrator to change this
Default value. You can also use the applicationtimeout attribute of the cfapplication tag to set application changes.
To overwrite the default asministrator settings.
Note: The prefix must be used when the application and session variables are used.
Server Variables
Server variables can be referenced by all customers in all applications served by the current web server
Disable ColdFusion Server.
Server variables are suitable for storing data that is not frequently changed and can be shared by multiple customers and applications. You can
Use server variables to store information required by all applications running on your site, such
Source information.
ColdFusion has the following built-in server variables:
· Server. ColdFusion. productname --- return the ColdFusion product name.
· Server. ColdFusion. productversion --- return ColdFusion version information.
· Server. ColdFusion. productlevel --- return the product level information of coldfusion.
· Server. ColdFusion. serialnumber --- return the serial number of coldfusion.
· Server. OS. Name --- return the name of the server operating system.
· Server. OS. additionalinformation --- return additional information about the operating system.
· Server. OS. Version --- return the version information of the server operating system.
· Server. OS. buildnumber --- return the establishment Number of the server operating system.
Note: Server variables have read-write attributes, so be careful not to overwrite the built-in server variables.
CGI Environment Variable
When a browser requests a service from a Web server, it creates a series of environment variables, some of which are created by the browser,
Some are created by the server. These variables are referred to as CGI variables and are referenced with the prefix "cgi ".
APIS instead of CGI and ColdFusion ).
Environment variables are some data related to the dialog between the browser and the server, such as IP addresses and browser types.
The name of the authenticated customer. You can reference CGI Environment Variables anywhere in the file requested by the browser. All
All CGI variables are read-only.
Note that the CGI variables that can be referenced in your application are related to the specific browser and web server software.
Check CGI variables
Because not every browser supports all CGI variables, ColdFusion
Always returns true, even if the browser does not support this variable. The method to avoid this is to detect an empty string,
Instead of checking logical values, for example:
<Cfif CGI. varname is not "">
CGI variable exists
CGI variable does not exist
Server CGI variable
The following table lists the most common CGI variables created by the server (not all browsers support these variables ):
Server CGI variable
The name and version of the server software that responds to the request. Format: Name/version.
The host name, DNS alias, or IP address of the server.
The version of the CGI specification that the server complies. Format: CGI/version.
The name and version of the protocol used in this request. Format: Protocol/version
The port number to which the request is sent.
The method used to request services. For HTTP, the methods include get, Head, and post.
The special path information provided by the browser. The Access Script can use a virtual path followed by a special path,
This information exists in path_info.
The path_info converted by the server. The virtual path is mapped to the physical path.
Specifies the virtual path of the script to be executed, which is used to reference the URL.
"?" In the URL The query information.
The Host Name of the request service. If the server does not know this information, it will create remote_addr instead
The IP address of the remote host requesting the service.
If the server supports customer authentication and scripts are protected, this variable contains authentication methods based on specific protocols.
If the server supports customer authentication and the script is protected, this variable contains the authenticated customer name.
If the HTTP server supports the rfc931 identifier, this variable is set to the remote customer name.
For queries with additional information, such as http post and put, this variable specifies the type of additional information.
The length of the information provided by the customer.
Customer CGI variable
The following table lists the most common CGI Environment variables that are passed to the server after being created by the browser:
Customer CGI variable
Referenced document. This is a link to the source document or the document that submits form data.
The browser currently used by the customer. Format: Software/version Library/version.

Use URL and form to pass variables

Use URL and form to pass variables
As mentioned above, using client variables, session variables, Application variables, and so on, you can
Data is transmitted between pages. The following describes how to use URL and form to pass variables. The two methods can be found on the webpage
To transfer local variables.
Passing Parameters Using URLs
You can append the parameter to the URL and pass it to the target file pointed to by the URL. Parameters to be passed with a question mark
The number is separated from the URL file address. The variable name and its value are appended to the question mark, so you can
Reference this variable, which is the URL variable described above. For example:
<A href = "example. cfm? User_id = 5 & color = # mycolor # ">
In this example, the variable user_id has a value of 5, and the variable color. The value is the value of expression # mycolor #.
Hand the file example. cfm referred to by the hyperlink.
In the target file example. cfm, You can reference these two changes in the form of URL. user_id and URL. color.
Quantity, such:
Your User ID is # URL. user_id # And
Your favorite color is # URL. mycolor #.
When passing parameters through a URL, pay attention to the following points:
· Use a question mark to separate the URL address from the parameter to be passed.
· Separate different parameters with the symbol.
· Do not use spaces. Some browsers will cut the URL when detecting spaces.
· Do not use special characters, such &,?,., And #.
If the value you want to pass may contain spaces and special characters, use the urlencodeformat () function. For example:
For example:
<Cfset fullname = "bob smith">
Href = "printname. cfm? Fullname = # urlencodedformat (fullname) # ">
Click Here </a>
For more information about this function, see CFML language reference.
Passing parameters with Form
Generally, the form is used to receive the input from the customer, and then the form variable is used in the file referred to by the Action attribute to obtain
User input data for processing. You can also use hidden fields to pass parameters from one web page to another. Example
For example, the following form contains a hidden field named mermer_id, which is passed to the file example. cfm.
<Form action = "example. cfm" method = "Post">
<Input type = "hidden"
Name = "customer_id" value = "24">
<Input type = "Submit" value = "enter">
In the example. cfm file, you can reference this variable in form of Form. mermer_id.
You can also use hidden fields to pass dynamic parameters, such as query results. For example:
<Form action = "example. cfm" method = "Post">
<Cfoutput query = "getcustomer">
<Input type = "hidden" name = "customer_id"
Value = "# customer_id #">
<Input type = "Submit" value = "enter">
This chapter introduces ColdFusion variables. The main point is the reference range and survival period of various variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.