View comments | RSS feed

onRequestStart

Description

Runs when a request starts.

Syntax

<cffunction name="onRequestStart" returnType="boolean">
   <cfargument type="String" name="targetPage" required=true/>
   ...
   <cfreturn Boolean>
</cffunction>

See also

onRequest, onRequestEnd, Method summary, Managing requests in Application.cfc in ColdFusion MX Developer's Guide

Parameters

ColdFusion MX passes the following parameters to the method:

Parameters Description

targetPage

Path from the web root to the requested page.

Returns

A Boolean value. Return False to prevent ColdFusion from processing the request. You do not need to explicitly return a True value if you omit the cffunction tag returntype attribute.

Usage

This method runs at the beginning of the request. It is useful for user authorization (login handling), and for request-specific variable initialization, such as gathering performance statistics.

If this method throws an exception (for example, if it uses the cfthrow tag), ColdFusion handles the error and does not process the request further.

If you call this method explicitly, ColdFusion does not start a request, but does execute the method code.

This method can access the requested page's Variables scope only if the Application.cfc file includes an onRequest method that calls the page. You can use Request scope variables to share data with the requested page even if Application.cfc does not have an onRequest method.

Example

This example uses the authentication code generated by the ColdFusion MX Dreamweaver Login wizard to ensure that the user is logged in. For Beta 2, the wizard generates code that is appropriate for Application.cfm only. To use this code with the Application.CFC, delete the generated Application.CFM

<cffunction name="onRequestStart"> 
   <cfargument name="requestname" required=true/>
   <!--- Authentication code, generated by the Dreamweaver Login wizard.
   <cfinclude template="mm_wizard_application_include.cfm">
   <!--- Regular maintenance is done late at night. During those hours, tell
          people to come back later, and do not process the request further. --->
   <cfscript>
      if ((Hour(now()) gt 1) and (Hour(now()) lt 3)) {
         WriteOutput("The system is undergoing periodic maintenance. 
            Please return after 3:00 AM Eastern time.");
         return false;
      } else {
         this.start=now();
         return true;
      }
   </cfscript>
</cffunction>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


eapostol said on Jun 19, 2005 at 7:51 PM :
Printing a simple loop as demonstrated with this code, within the onRequestStart function:

<cfscript>

writeOutput("begin<br />");
for(i in this.onRequestStart){
currMethod=this.onRequestStart;
if(i NEQ "next"){
currValue=currMethod[i];
if(NOT(isStruct(currValue))){
writeOutput(i & "=" & currValue & "<br />");
}else{
writeOutput(i & "=not a string value<br />");
}
}else{
// handle the value next?

}
}

</cfscript>

prints out the following values:

Output=true
Metadata=not a string value
PagePath=C:\Inetpub\wwwroot\cfmx7_test\Application.cfc
Access=public
SuperScope=not a string value
ReturnType=any

There is also a NEXT property of type "class". Can someone explain what these are used for? I cannot find references to them in the manual.
Oblio said on Jan 30, 2007 at 9:57 AM :
When trying to use getPageContext().forward() in the onRequestStart method, you must use a check and return false for the forward to work. This will prevent a loop on the target page.

For example, if you want to forward to "login.cfm", you must do:
<cfif arguments.targetPage neq "/login.cfm">
<cfscript>
getPageContext().forward("login.cfm");
</cfscript>
<cfreturn false />
<cfelse>
<cfreturn true />
</cfif>
(*note, this will prevent any other page from running without a bit of modification)

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000700.htm