Reputation: 35
I am very new to ColdFusion (started yesterday). I am trying to create a session variable in checklogin.cfm
, which equals the username input from form. So I can display the username on result page. Is it possible to do so?
Or is there any better way to do this?
application.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>
home.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>
login.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>
result.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>
The problem is with the result page, where it says Element USERNAME is undefined in SESSION
pointing to line <b><font size="6">Hi #session.userName#! </font></b>
Upvotes: 3
Views: 733
Reputation: 13548
To fix the error mentioned you need to move the setting of the session variable above the <cflocation>
tag in your check_login.cfm file.
From this:
<!--- 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/>
To something like this:
<!--- 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>
The reason for this is because ColdFusion will stop processing the rest of your file when it encounters the <cflocation>
tag. When it processes that tag it will immediately send a redirect back to the user. So your code to set the session variable was never running.
Updated example with using CFLOCK around the setting of the session variable
<!--- 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>
Upvotes: 5