Jack
Jack

Reputation: 1079

session not working in coldfusion

I am working in a Login.cfm file and using the followign approach for login

<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#"

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?

Please clarify

Upvotes: 0

Views: 760

Answers (2)

jk.
jk.

Reputation: 14435

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.

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.

For more information on locking session variables:

Should I always need to use cflock with SESSION scope variables?

Configuring and using session variables

Upvotes: 0

Dan Bracuk
Dan Bracuk

Reputation: 20794

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.

  1. form.username has to be defined
  2. the variable inpUserName, without white space and in lower case has to be "myusername"
  3. the variable inpPassword, without white space and in lower case has to be "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. That's probably not what you had in mind when you wrote that code.

Upvotes: 1

Related Questions