3

我对 ColdFusion 很陌生(昨天开始)。我正在尝试在中创建一个会话变量checklogin.cfm,它等于从表单输入的用户名。所以我可以在结果页面上显示用户名。有可能这样做吗?或者有没有更好的方法来做到这一点?

应用程序.cfm

<cfset this.name = "Name of your application">
<cfset this.sessionManagement = true>

check_login.cfm

<!--- If username equals user1 and password equals password1, take user to home page. ---> 
<cfif form.userName eq "user1" AND form.password eq "password1">
  <cfset session.userName = form.userName/>
  <cflocation url="home.cfm" ADDTOKEN="Yes">
  <!--- If username doesn't equal user1 and/or password doesn't 
    equal password1, take user back to login page. ---> 
<cfelse>
  <cflocation url="login.cfm" ADDTOKEN="Yes">  
</cfif>

主页.cfm

<cfoutput>
    <br>
     <b><font size="6">Enter the numbers you want to add:</font></b>
</cfoutput>

<!--- Add numbers form ---> 

<cfform action="result.cfm" method="post"> 
    <!--- Get numbers from user ---> 
    <p> 
        Enter first number: 
        <cfinput type="Text" name="number1" required="Yes" size="20"
          maxlength="100" message="You must enter a number.">
        <br> 

        Enter second number: 
        <cfinput type="Text" name="number2" required="Yes" size="20"
          maxlength="100" message="You must enter a second number.">
        <br>
    </p> 

    <!--- submit button ---> 
    <cfinput type="Submit" name="submitForm" value="Submit"> 
</cfform> 

登录.cfm

<cfform action="check_login.cfm" method="post"> 
    <!--- Get login info from user ---> 
    <p> 
        UserName: 
        <cfinput type="Text" name="userName" required="Yes" size="20" maxlength="25" 
          message="Username is required and must be less than 25 characters.">
        <br> 

        Password: 
        <cfinput type="password" name="password" required="Yes" size="20" maxlength="20" 
          message="Password is required and must be less than 20 characters.">
        <br>
    </p> 

    <!--- submit button ---> 
    <cfinput type="Submit" name="submitForm" value="Submit"> 
   </cfform> 

结果.cfm

<cfset result = form.number1 + form.number2 >
<cfoutput>
     <b><font size="6">Hi #session.userName#! </font></b><br> 
     <br>
    <!-- Display result to user. -->
    <font size="5">Your result is: #result#</font>
</cfoutput>

问题出在结果页面上,它说Element USERNAME is undefined in SESSION指向行<b><font size="6">Hi #session.userName#! </font></b>

4

1 回答 1

5

要修复提到的错误,您需要将会话变量的设置移动到<cflocation>check_login.cfm 文件中的标记上方。

由此:

<!--- If username equals user1 and password equals password1, take user to home page. ---> 
<cfif form.userName eq "user1" AND form.password eq "password1">
    <cflocation url="home.cfm">
<!--- If username doesn't equal user1 and/or password doesn't equal password1, take user back to login page. ---> 
<cfelse>
    <cflocation url="login.cfm">  
</cfif>
<cfset session.userName = form.userName/>

对于这样的事情:

<!--- If username equals user1 and password equals password1, take user to home page. ---> 
<cfif form.userName eq "user1" AND form.password eq "password1">
    <cfset session.userName = form.userName/>
    <cflocation url="home.cfm">
<!--- If username doesn't equal user1 and/or password doesn't equal password1, take user back to login page. ---> 
<cfelse>
    <cflocation url="login.cfm">  
</cfif>

这样做的原因是,当 ColdFusion 遇到<cflocation>标签时,它会停止处理文件的其余部分。当它处理该标签时,它将立即将重定向发送回用户。因此,您设置会话变量的代码从未运行过。

更新了在会话变量设置周围使用 CFLOCK 的示例

<!--- If username equals user1 and password equals password1, take user to home page. ---> 
<cfif form.userName eq "user1" AND form.password eq "password1">
    <cflock scope="session" type="exclusive" timeout="10">
        <cfset session.userName = form.userName/>
    </cflock>
    <cflocation url="home.cfm">
<!--- If username doesn't equal user1 and/or password doesn't equal password1, take user back to login page. ---> 
<cfelse>
    <cflocation url="login.cfm">  
</cfif>
于 2018-12-03T18:03:58.350 回答