×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Login function with CF

How can I create a login/logout feature for my site?? by GUJUm0deL
Posted: 18 Apr 03 (Edited 12 Jan 06)

A lot of users have posted how they can have a secure login feature on their site, you can use ASP, CGI, PHP and Coldfusion...
  This is the coldfusion way (its very easy):

To make a login feature

1)  Create a Application.cfm page, in there have this:

//establishes the SESSION variables here...
<cfapplication name=&quot;nameoffile&quot;
  applicationtimeout=&quot;#CreateTimeSpan(0,0,5,0)#&quot;
  sessionmanagement=&quot;yes&quot;
  setclientcookies=&quot;yes&quot;>


<cfif not isdefined(&quot;session.loggedin&quot;)>
  <cfset session.loggedin = &quot;false&quot;>
</cfif>

<cfset currentpage = getfilefrompath(&quot;#CGI.CF_TEMPLATE_PATH#&quot;)>
  <cfif session.loggedin eq &quot;false&quot;>
    <cfif &quot;#VARIABLES.currentPage#&quot; is not &quot;index.cfm&quot; or
    &quot;#VARIABLES.currentPage#&quot; is not &quot;register.cfm&quot; or
    &quot;#VARIABLES.currentPage#&quot; is not &quot;login.cfm&quot;>
    <cflocation url=&quot;no_login.cfm&quot;>
  <cfelseif     
        &quot;#VARIABLES.currentPage#&quot; is &quot;index.cfm&quot; or
    &quot;#VARIABLES.currentPage#&quot; is &quot;login.cfm&quot;>
    <cfexit method=&quot;EXITTAG&quot;>
  </cfif>
<cfelse>
    <cfexit method=&quot;EXITTAG&quot;>
</cfif>


*NOTE:  In the <cfif> part (is not) you list the cfm pages that you do not want the user to access without them logging in, even if they directly enter the cfm file name into the url...if the user has not loged in they get redirected to a no_login.cfm page (the above part in green)...
  In the <cfelseif> part (is) you list the cfm pages that you want to give the access to...
  This way your pages are &quot;protected&quot; from anyone that has not logged in...

2)  Then create a login.cfm page, and have this:

<cfquery name=&quot;IsValidLogin&quot; datasource=&quot;dBName&quot;>
  SELECT *
  FROM Table1
  WHERE user_name = '#form.user_name#' AND password = '#form.password#'
</cfquery>

<cfif IsValidLogin.RecordCount EQ 0>
  <cflock scope=&quot;SESSION&quot; type=&quot;EXCLUSIVE&quot; timeout=&quot;2&quot;>
    <cfset SESSION.LOGGEDIN = &quot;false&quot;>
    <cfset SESSION.USER=&quot;&quot;>
  </cflock>
<cflocation url=&quot;no_login.cfm?action=fail&quot;>

<cfelseif form.user_name EQ 'someName'>
  <cflock scope=&quot;SESSION&quot; type=&quot;EXCLUSIVE&quot; timeout=&quot;2&quot;>
    <cfset SESSION.LOGGEDIN = &quot;true&quot;>
    <cfset SESSION.USER=&quot;#form.user_name#&quot;>
  </cflock>
    <cfoutput><cflocation url=&quot;Admin.cfm&quot;></cfoutput>

<cfelse>
  <cflock scope=&quot;SESSION&quot; type=&quot;EXCLUSIVE&quot; timeout=&quot;2&quot;>
    <cfset SESSION.LOGGEDIN = &quot;true&quot;>
    <cfset SESSION.USER=&quot;#form.user_name#&quot;>
  </cflock>
<cfoutput><cflocation url=&quot;user.cfm?user_name=#IsValidLogin.user_name#&quot;></cfoutput>
</cfif>


*NOTE:  the code for <cfelseif form.user_name EQ 'someName'> means that if an admin signs in they get directed to a special page, with options set for Admins.  Otherwise direct them to the user.cfm page (only if they signed in), and if not signed in then direct them to the no_login.cfm page...

3)  Create a no_login.cfm page, and have this:

<cfif IsDefined(&quot;URL.action&quot;)>
<div align=&quot;center&quot;>
  <p> </p>
  <p> </p>
  <p><b>Please enter a valid user name and passord combination.</b></p>
  <p><b>Please click here to try again: <a href=&quot;index.CFM&quot;>Login</a></b></p>
</div>


4)  Lastly, create a index.cfm page, and create the longin form.  Make sure the action is set to login.cfm page (like this:  action=&quot;login.cfm&quot;)
    This is only cause you want the server to check that the user signed in AND/OR the username/password combo. exists.


To make a logout feature

1) to logout, create a logout.cfm file and have this code there:

<CFSCRIPT>structclear(SESSION);</CFSCRIPT>
<cflocation url="index.cfm">

  Add logout.cfm as a link to any page that needs it...this will clear the SESSION variable making it seem like the user never logged in...
  The reason I add this script in another file is if you have more then one page that needs a logout function then you don't need to add this script to all those pages...plus if the <cflocation> page ever changes then you just change it on the logout.cfm file (as opposed to changing it on numorous files)...

And that's it!!  Try it out, ig you have any questions/comments email me or make a post in the forum...

Back to Adobe FAQ Index
Back to Adobe Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close