简体   繁体   English

会话在Coldfusion中不起作用

[英]session not working in coldfusion

I am working in a Login.cfm file and using the followign approach for login 我正在使用Login.cfm文件并使用Followign方法进行登录

<cfif IsDefined("FORM.inpUserName") AND ((LCASE(TRIM(inpUserName)) IS "myusername" AND inpPassword IS "mypassword") )>
    <cfset session.username = FORM.inpUserName />
    <cfset SESSION.LoggedIn = 1>    
    <cflocation url="index.cfm" addtoken="no">

<cfelse>
    <cfset SESSION.LoggedIn = 0>
</cfif>

<cfparam default="" name="inpUserName" />
<cfparam default="" name="inpPassword" />

The form is defined as follows: 表格定义如下:

<cfform action="Login.cfm" method="post" and so on ...

Inside cfform, I have defined two cfinput tags capturing the information from user with name attribute as name="inpUserName" and value="#inpUserName#" 在cfform中,我定义了两个cfinput标记,用于捕获来自用户的信息,其名称属性为name =“ inpUserName”和value =“#inpUserName#”

and similarly for password field. 和类似的密码字段。

When I click on Login button nothings is happening, shoudln't it be going to index.cfm as I have mentioned at the top in cflocation tag? 当我单击“登录”按钮时,什么都没有发生,是否应该像我在cflocation标签顶部提到的那样去index.cfm?

Please clarify 请澄清

Let's look at this conditional: 让我们看看这个条件:

<cfif IsDefined("FORM.inpUserName") 
AND ((LCASE(TRIM(inpUserName)) IS "myusername" 
AND inpPassword IS "mypassword") )>

That's looking for 3 things to be true. 这是在寻找3件真实的东西。

  1. form.username has to be defined form.username必须定义
  2. the variable inpUserName, without white space and in lower case has to be "myusername" 变量inpUserName(无空格,小写)必须为“ myusername”
  3. the variable inpPassword, without white space and in lower case has to be "mypassword" 变量inpPassword(无空格,小写)必须为“ mypassword”

This means the only way your cfif conditional can be satisfied is if you enter values of "myusername" and "mypassword" when you submit the form. 这意味着可以满足cfif条件的唯一方法是,在提交表单时输入“ myusername”和“ mypassword”的值。 That's probably not what you had in mind when you wrote that code. 编写该代码时,您可能没有想到这一点。

Try cleaning it up a bit and clarifying the scope of your form variables uniformly throughout: 尝试清理一下,并在整个过程中统一阐明表单变量的范围:

<cfparam name="form.inpUserName" default="" />
<cfparam name="form.inpPassword" default="" />

<cfif TRIM(form.inpUserName) IS "myusername" AND form.inpPassword IS "mypassword">
    <cflock type="exclusive" scope="session" timeout="10" >
        <cfset session.username = form.inpUserName />
        <cfset session.LoggedIn = 1 />   
    </cflock> 
    <cflocation url="index.cfm" addtoken="no" />

<cfelse>
    <cflock type="exclusive" scope="session" timeout="10" >
        <cfset session.LoggedIn = false /> 
    </cflock>
</cfif>

<cfinput type="text" name="inpUserName" value="#form.inpUserName#" />
<cfinput type="password" name="inpPassword" value="#form.inpPassword#" />

You don't need the isDefined function if you are setting cfparam vars. 如果要设置cfparam vars,则不需要isDefined函数。

It should now go to the index.cfm page if you enter "myusername" in the username field and "mypassword" in the password field on submit provided it posts back to itself. 现在,如果您在用户名字段中输入“ myusername”,并在密码字段中输入“ mypassword”(如果它回发了),则应该转到index.cfm页面。

For more information on locking session variables: 有关锁定会话变量的更多信息:

Should I always need to use cflock with SESSION scope variables? 是否应该始终将cflock与SESSION范围变量一起使用?

Configuring and using session variables 配置和使用会话变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM