Custom Search

Thursday, January 24, 2008

Tutorial : User Login

First create the application.cfm file in your root folder,then write this code.

Application.cfm
 <cfapplication name="cfcentral"
    sessionmanagement="true"
    sessiontimeout="#CreateTimeSpan(0,0,30,0)#"
    />
  <cfset application.dsname="suman">
  <cfset request.dsname="suman">
  <cfset session.profile=StructNew()>
 
 <cfif IsDefined("Form.logout")>
  <cflogout>
 </cfif>
 
 <cflogin>
  <cfif NOT IsDefined("cflogin")>
   <cfinclude template="loginform.cfm">
   <cfabort>
  <cfelse>
   <cfif cflogin.name IS "" OR cflogin.password IS "">
    <cfoutput>
  <h2>You must enter text in both the User Name and Password fields.     </h2>
    </cfoutput>
    <cfinclude template="loginform.cfm">
    <cfabort>
   <cfelse>
    <cfquery name="loginQuery" dataSource="#request.dsname#">
    SELECT AdminName, Role
    FROM admin
    WHERE
  AdminName = '#cflogin.name#'
  AND Password = '#cflogin.password#'
    </cfquery>
    <cfif loginQuery.Role NEQ "">
  <cfloginuser name="#cflogin.name#" Password = "#cflogin.password#"
   roles="#loginQuery.Role#">
    <cfelse>
  <cfoutput>
   <H2>Your login information is not valid.<br>
   Please Try again</H2>
  </cfoutput> 
  <cfinclude template="loginform.cfm">
  <cfabort>
    </cfif>
   </cfif> 
  </cfif>
 </cflogin>
 
 <cfif GetAuthUser() NEQ "">
  <cfoutput>
   <form action="#CGI.script_name#?#CGI.query_string#" method="Post">
    <input type="submit" Name="Logout" value="Logout">
   </form>
  </cfoutput>
 </cfif>
   
Second create loginform.cfm file in your root folder.

loginform.cfm
 Please Log In
 
 <cfoutput>
    <form name="LoginForm" action="#CGI.script_name#?#CGI.query_string#" method="Post">
    <table>
    <tr>
    <td>username:</td>
    <td><input type="text" name="j_userName"></td>
    </tr>
    <tr>
    <td>password:</td>
    <td><input type="password" name="j_password"></td>
    </tr>
    </table>
    <br>
    <input type="submit" value="Log In">
    </form>
 </cfoutput>
   
Craete a table name admin with AdminName,Password,Role field.

WOW its done