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;

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

<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;>
        &quot;#VARIABLES.currentPage#&quot; is &quot;index.cfm&quot; or
    &quot;#VARIABLES.currentPage#&quot; is &quot;login.cfm&quot;>
    <cfexit method=&quot;EXITTAG&quot;>
    <cfexit method=&quot;EXITTAG&quot;>

*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;>
  FROM Table1
  WHERE user_name = '#form.user_name#' AND password = '#form.password#'

<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;>
<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;>
    <cfoutput><cflocation url=&quot;Admin.cfm&quot;></cfoutput>

  <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;>
<cfoutput><cflocation url=&quot;user.cfm?user_name=#IsValidLogin.user_name#&quot;></cfoutput>

*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>

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:

<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