Skip to main content
March 30, 2009
Question

Application log In

  • March 30, 2009
  • 18 replies
  • 1308 views
Why does the attached code not cause people to redirect to the login.cfm page if there is no accessAllowed session variable? If they log in and are successfull it sets the variable accessAllowed = true which means the user is authenticated however it stopped working and the only thing I can see that's changed is this code.
    This topic has been closed for replies.

    18 replies

    Inspiring
    March 31, 2009
    try this code:

    <cffunction name="onRequestStart" access="public" returntype="boolean"
    output="true">
    <cfargument name="TargetPage" type="string" required="yes" />
    <cfset var qsStruct = oRequest.getParameterMap()>
    <cfif structKeyExists(qsStruct,'clear')>
    <cfset structClear(session)>
    <cfset onApplicationStart()>
    </cfif>
    <!--- check to see if logged in --->
    <cfif (NOT structKeyExists(session,'accessAllowed')) OR
    session.accessAllowed IS false>
    <!--- if not logged in and not on login page, redirect to login --->
    <cfif listLast(cgi.script_name,'/') neq 'login.cfm'>
    <cflocation url="login.cfm" addtoken="no">
    <cfabort>
    </cfif>
    </cfif>
    <cfreturn true>
    </cffunction>


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    March 30, 2009
    <cffunction name="onRequestStart" access="public" returntype="boolean"
    output="true">
    <cfargument name="TargetPage" type="string" required="yes" />
    <!--- check to see if logged in --->
    <cfif (NOT structKeyExists(session,'accessAllowed')) OR
    session.accessAllowed IS false>
    <!--- if not logged in and not on login page, redirect to login --->
    <cfif listLast(cgi.script_name,'/') neq 'login.cfm'>
    <cflocation url="login.cfm">
    <!--- <cfabort>
    you can also use <cfset getPageContext().forward('login.cfm')>
    instead of above 2 lines --->
    </cfif>
    </cfif>
    <cfreturn true>

    <cfif structKeyExists(url,'clear')><cfset onApplicationStart()></cfif>
    <cfif structKeyExists(url,'clear')><cfset structClear(session)></cfif>
    </cffunction>
    Inspiring
    March 30, 2009
    that would be because session.allowAccess exists and is true.

    where exactly do you have this line:
    <cfif structKeyExists(url,'clear')><cfset structClear(session)></cfif>
    ?

    instead of posting snippets from different parts of your onRequestStart
    method, why don;t you post all of it? the order you have various <cfif>
    clauses inside of it may be very important...

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    March 30, 2009
    Azadi your log in code worked once then it didn't

    Inspiring
    March 30, 2009
    you mean you are restarting your whole application (well,
    re0initializing really) every time a user logs out???

    but, really, if it is just a user logging out, why would you want to
    call onApplicationStart()???

    how else to do it would depend, to start with, on how you log your users
    in...


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    March 30, 2009
    I have this to process a log out how else could I do it?

    <cfif structKeyExists(url,'clear')><cfset onApplicationStart()></cfif>
    <cfif structKeyExists(url,'clear')><cfset structClear(session)></cfif>
    </cffunction>
    Inspiring
    March 30, 2009
    try this:

    <cfif (NOT structKeyExists(session,'accessAllowed')) OR
    session.accessAllowed IS false>

    you may also need a <cfabort> after your <cflocation> tag to prevent
    processing of originally requested page...

    also, onRequestStart() method should accept one argument - targetpage.

    try this for a full code:

    <cffunction name="onRequestStart" access="public" returntype="boolean"
    output="true">
    <cfargument name="TargetPage" type="string" required="yes" />
    <!--- check to see if logged in --->
    <cfif (NOT structKeyExists(session,'accessAllowed')) OR
    session.accessAllowed IS false>
    <!--- if not logged in and not on login page, redirect to login --->
    <cfif listLast(cgi.script_name,'/') neq 'login.cfm'>
    <cflocation url="login.cfm">
    <cfabort>
    <!--- you can also use <cfset getPageContext().forward('login.cfm')>
    instead of above 2 lines --->
    </cfif>
    </cfif>
    <cfreturn true>
    </cffunction>



    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    March 30, 2009
    This is obviously in the application.cfc?

    well, instead of using <cflocation> you could simply set request.targetpage to the login.cfm ...

    Let me know if it works

    Martin