in coldfusion, variables in what scope can be pass

2019-07-24 19:24发布

问题:

i wrote 2 pages to test this problem, but the server complaints error. i don't know why, anyone can explaint it? great thanks.

this is 1.cfm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Page Title</title>
    </head>
    <body>
        <cfscript>
            a="aaaaaaaaaaa";
            b="bbbbbbbbbbb";

            request.r1="rrrrrrr111111111";
            request.r2="rrrrrrrr222222222";

            session.s1="sssssssssss11111111111";
            session.s2="sssssssssss2222222222";
        </cfscript>
        <iframe src="2.cfm" width="600" height="400" name="myframe" scrolling="yes">
        </iframe><br />
        variables
        <cfdump var="#variables#">
        request
        <cfdump var="#request#">
        session
        <cfdump var="#session#">
    </body>
</html>

and this is 2.cfm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>2.cfm</title>
    </head>
    <body>
        variables
        <cfdump var="#variables#">
        request
        <cfdump var="#request#">
        session
        <cfdump var="#session#">
    </body>
</html>

回答1:

It seems like you're misunderstanding a basic concept of web-page requests.

An iframe, while displayed as a portion of the rendering page, is in fact its own request, entirely separate from the original page request.

Session variables would be shared between the two of them (assuming you have sessions enabled in Application.cfm/Application.cfc), and although it's unlikely that you'll get into a race condition by setting variables from a parent page (1.cfm) and reading them from a child page in an iframe (2.cfm), it's just not a great idea (best practice).

Request variables set in the parent page (1.cfm) will not be available to the page in the iframe (2.cfm), as it is a separate request.

Like the Request scope is private to each request (but shared to all templates and objects), the "variables" scope is private to each template (but shared among them when using cfinclude).

While your iframe will have access to its own request and variables scopes, they will not be the same scope as the original page (1.cfm).

This is a fairly basic concept of programming in general, and also of ColdFusion. If you're finding it difficult to grasp, you might consider picking up a copy of the ColdFusion Web Application Construction Kit, which can take you from complete novice to beginner-intermediate level fairly quickly.



回答2:

Do you have an Application.cfm in the directory you're running these tests in?

If you add the following line into a file called Application.cfm and the root of the directory it should work.

<cfapplication name="test_app" sessionmanagement="true">

I tested your two files and without the Application.cfm it broke, with it present it works fine.



回答3:

I think Ian's on the right track here with his observation that to use session variables, one needs to have sessionmanagement enabled, however I think suggesting using Application.cfm for this is a bit anachronistic.

If one is using a version of CF from CFMX7 onwards, the recommended way to manage the application framework is via Application.cfc, and the equivalent to Ian's code would be:

<cfcomponent>

    <cfset this.name = "test_app">
    <cfset this.sessionManagement = true>

</cfcomponent>