Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to creat a login/password page in coldFuion

Status
Not open for further replies.

lminmei

Programmer
Feb 1, 2000
111
US
how do i creat the validation page that validates a userid and password from a database?
 
Just create a form like:<br><br><FONT FACE=monospace>&lt;form action=&quot;login.cfm&quot; method=&quot;post&quot;&gt;<br>&nbsp;&nbsp;UserID&lt;input type=&quot;text&quot; name=&quot;UserID&quot;&gt;&lt;br&gt;<br>&nbsp;&nbsp;Password&lt;input type=&quot;password&quot; name=&quot;password&quot;&gt;&lt;br&gt;<br>&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<br>&lt;/form&gt;</font><br><br>On the action page, do something like:<br><br><FONT FACE=monospace>&lt;cfquery name=&quot;CheckUser&quot; db=&quot;mydb&quot;&gt;<br>&nbsp;&nbsp;select * from Users<br>&nbsp;&nbsp;where UserID='#form.UserID#' <br>&nbsp;&nbsp;&nbsp;&nbsp;and Password='#form.Password#'<br>&lt;/cfquery&gt;<br><br>&lt;cfif CheckUser.RecordCount&gt;<br>&nbsp;&nbsp;&lt;cfset session.authenticated=1&gt;<br>&nbsp;&nbsp;&lt;cflocation url=&quot;userstart.cfm&quot; addtoken=&quot;yes&quot;&gt;<br>&lt;cfelse&gt;<br>&nbsp;&nbsp;&lt;cfoutput&gt;You have input your user information incorrectly, please hit the back button and try again&lt;/cfoutput&gt;<br>&lt;/cfif&gt;</font><br><br>On all of your password protected pages, just check for the existence of the variable session.authenticated:<br><br><FONT FACE=monospace>&lt;cfif not isdefined(&quot;session.authenticated&quot;)&gt;<br>&nbsp;&nbsp;&lt;cfoutput&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;You are not authenticated, or your session has timed out<br>&nbsp;&nbsp;&lt;/cfoutput&gt;<br>&nbsp;&nbsp;&lt;cfabort&gt;<br>&lt;/cfif&gt;</font><br><br>You could also redirect the user to your home page, or whatever.<br><br>The most important thing to remember, though, is you have got to put <b>?cfid=#cfid#&cftoken=#cftoken#</b> (and that has to be in &lt;cfoutput&gt;&lt;/cfoutput&gt; tags) at the end of ALL of the links in your password protected area that go to other pages in that area, or you will lose the session.authenticated variable......<br><br>Hope this helps....
 
i tried to write register.cfm and login.cfm file according to what you wrote above.<br>login.cfm shows erro :<br>&quot;Attempt to access a Session variable when session management is not enabled. Use the CFAPPLICATION tag to enable session management.<br><br>Note: This feature may have been disabled by the site administrator.<br><br>The error occurred while evaluating the expression: session.authenticated=1&quot;<br>
 
Create a file named Application.cfm and put this tag in it:<br><br><FONT FACE=monospace>&lt;CFAPPLICATION NAME=&quot;MyApp&quot; CLIENTMANAGEMENT=&quot;Yes&quot; SESSIONMANAGEMENT=&quot;Yes&quot; SETCLIENTCOOKIES=&quot;No&quot;&gt;</font><br><br>This will turn on client and session management and make the other pages work properly....<br><br>Let me know if you have any further problems with this...<br><br>DM
 
when i put this tag into my login file, it works.<br>thanks
 
It'll work there, but you should really put in in your Application.cfm file.&nbsp;&nbsp;You can also create other application and session specific variables in this file.&nbsp;&nbsp;The reason you need to do this is the Application.cfm file will be run before the other templates and you'll be able to track your application and session variables more efficiently....
 
I'm also having problems with this.<br><br>I've got everything set up as you (DarkMan) suggested in your posts on April 27 and May 31 (including &quot;?cfid=#cfid#&cftoken=#cftoken# with all links), but I still get the &quot;An error occurred while evaluating the expression: session.authenticated=1<br><br>I have the Application.cfm file showing what you suggested and I also have the tags at the beginning of each protected page.<br><br>My CF server has Session variables enabled. <br><br>What have I done wrong?? <p>Peter Toby<br><a href=mailto:webmaster@petertoby.com.au>webmaster@petertoby.com.au</a><br><a href= site is under construction but getting better!
 
i put &lt;CFAPPLICATION ...into the same file with what he wrote before. it works, if i put &lt;CFAPPLICATION ..into a seperate file, it won't work.
 
hm...<br>According to what I know, &lt;CFAPPLICATION&gt; should be used in the Application.cfm file :)<br>I've always set session variable management in this file<br>& its works pretty well.<br>Perhaps the problem lies in other codings??
 
hm...<br>According to what I know, &lt;CFAPPLICATION&gt; should be used in the Application.cfm file :)<br>I've always set session variable management in this file<br>& its works pretty well.<br>Perhaps the problem lies in other codings??
 
The &lt;CFAPPLICATION&gt; tage needs to be in a seperate file named Application.cfm<br><br>As an aside, I generally do not like to use session variables for this.&nbsp;&nbsp;What I do is that after autheticating the user for the first time, I put the user's login and password into a cookie:<br><br>&lt;CFCookie NAME=&quot;MyUser&quot; VALUE=&quot;#UserID#,#Password#&quot;&gt;<br><br>This cookie will expire (and be purged) the next time the user exits his browser.<br><br>Then at the top of each page I do a CFINCLUDE to something like:<br><br>&lt;CFIF ParameterExists(cookie.myuser)&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfquery name=&quot;CheckUser&quot; db=&quot;mydb&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;SELECT * from Users<br>&nbsp;&nbsp;&nbsp;&nbsp;WHERE UserID='#ListFirst(cookie.myuser)#' <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and Password='#ListLast(cookie.myuser)#'<br>&lt;/cfquery&gt; <br>&lt;CFIF CheckUSer.RecordCount IS 0&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;CFLOCATION URL=&quot;LoginFailed.cfm&quot;&gt;<br>&lt;/cfif&gt;<br><br><br>
 
Darkman,
Thanks for all your help. My supervisor threw CF at me 3 weeks ago and today alone, you helped me twice. My many thanks to you and the other CF people here.

Brian
belefant@jhu.edu [sig][/sig]
 
dbarnhart,

The line:

<CFCookie NAME=&quot;MyUser&quot; VALUE=&quot;#UserID#,#Password#&quot;>

where abouts should that be put? in a separate file called cookie.cfm?

Could you also explain the advantage the way you do it over the other way?

Cheers,

Stuart.

 
I also noticed that if I have the file application.cfm in my directory all my title tags disappear... is there anyway to get around that?

Cheers,
 
dbarnhart,
yeah, cookies can be used..
session variables by default, times out in 20min.
But they can be adjusted to other values.
Having the cookies for user ID may not be a good idea,
bcoz if a user logs in and leaves the site for a long period of time, other people can 'take the chance' to navigate the site thru his ID..
So the session times out after 20min should prevent this cause..
Whereas cookies will only expire when u define it,
or when the browser is closed.

Saturday(Stuart),
nope, <cfcookie> can be declared anywhere..
there's no such cookie.cfm but
there's a manatory application.cfm file
and what u mean by 'all my title tags disappear..'?

Hope i answer ur question aboved :)


[sig][/sig]
 
Isn't there a problem using cookies for authentication because there may be a caching proxy between you and the client which will cache the resource and then serve it up to the next user with the set-cookie header already in the response?

Can and should this be solved by adding cache-control http headers to the response?
 
dlownsbr,

eh.. i'm not very sure abt that..
but usually, pple use session variables for user IDs
which i normally also do..
Guess it would be fine using juz session variables :)
[sig][/sig]
 
A small point which applies to any user input form - it's best to design it so it can handle both initial input (so the form starts empty) and correction of errors (so the form displays the previous inputs). So:
* the display page looks for values returned for reinput
* the action page checks the input and, if errors found re-invokes the display page and passes it the inout values plus error message(s).

N.B. To do this you must use <cfoutput> to build the form.

For example in Iminmei's log-in page:

<!--- LOGIN DISPLAY PAGE --->
<!--- Defaults in case this is the first time --->
<cfparam name=&quot;URL.ErrorMsg&quot; default=&quot;&quot;>
<cfparam name=&quot;URL.UserId&quot; default=&quot;&quot;>
<cfparam name=&quot;URL.Password&quot; default=&quot;&quot;>
<!--- Check for error message --->
<cfif URL.ErrorMsg NEQ &quot;&quot;>
<cfoutput>
#URL.ErrorMsg #
</cfoutput>
</cfif>
<!--- Initialise form from arguments received --->
<cfoutput>
<form action=&quot;login_action.cfm&quot; method=&quot;post&quot;>
UserId<input type=&quot;text&quot; name=&quot;UserId&quot; value=&quot;#URL.UserId#&quot;><br>
Password<input type=&quot;password&quot; name=&quot;Password&quot; value=&quot;#URL.Password#&quot;><br>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</cfoutput>

<!--- LOGIN ACTION PAGE --->
<cfquery name=&quot;CheckUser&quot; db=&quot;mydb&quot;>
select * from Users
where UserID='#form.UserId#'
and Password='#form.Password#'
</cfquery>

<cfif CheckUser.RecordCount>
<cfset session.authenticated=1>
<cflocation url=&quot;userstart.cfm&quot; addtoken=&quot;yes&quot;>
<cfelse>
<!--- Return the inputs as URL parameters. --->
<cfset RedisplayLogin = &quot;login_display.cfm?ErrorMsg=You have input your user information incorrectly, please hit the back button and try again&UserId=#form.UserId#&Password=#form.Password#&quot;>
<cflocation url=&quot;#RedisplayLogin#&quot;>
</cfif>

There are other ways to manage the communication between the dispay page and the action page. Fusebox is becoming very popular - see [sig][/sig]
 
Hi there, I need to ask this. Do we need to have 2 application.cfm --one for the login and the other for the logout? Is this true? Coz I only have one application for the login and not for the logout.Please help.....

Thanx
JanJul [sig][/sig]
 
hi ! my question is, if i don't want to use the cfauthenticate tags (well, and even if i wanted to), why do i have to put ?cfid=#cfid#&cftoken=#cftoken# at the end of ALL of the links ?? i mean, cfid and cftoken (and urltoken) are session variable, right ? so why can't i access them from any page of my application ??
i've tried to set a session variable in the Application.cfm (say, Session.my_id) ... once set, if i move to an another page, then i can't access that session variable any more ... just like if it had never been defined ... is this that you can't set session variables in the Application.cfm ??? or what ?? [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top