Solved

Cflocation question

Posted on 2012-03-15
8
730 Views
Last Modified: 2012-03-16
Hi,

I am trying to perform a cflocation tag inside of the application.cfc within the onRequestStart function and keep getting an error.

If I leave out the cflocation tag from within the conditional recordcount tag, the pages will open fine. But once I include the cflocation tag, I get an error.

I am trying to run a query on every page to check some details about a User. If the checkuser function comes back with a recordcount greater than 0, i want to direct the user to a specific page no matter where in the site the user was browsing.

I would appreciate it if anyone has a clue why I am getting an error.

Thanks!

Code Sample Below:

<cffunction name="onRequestStart" output="false" returnType="void">

<cfinvoke component="#APPLICATION.somepath#" method="checkUser"  returnvariable="checkUser">
<cfinvokeargument name="User_code" value="#SESSION.User_code#">
</cfinvoke>

         <cfif checkUser.recordcount GT 0>
           
          <cflocation url="../index.cfm" addtoken="no">
                 
            </cfif>


</cffunction>
0
Comment
Question by:CFbubu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 37728838
make the methods returntype boolean and it will work

<cffunction name="onRequestStart" returnType="boolean" output="false">
  <cfargument name="thePage" type="string" required="true">

<cfinvoke component="#APPLICATION.somepath#" method="checkUser"  returnvariable="checkUser">
<cfinvokeargument name="User_code" value="#SESSION.User_code#">
</cfinvoke>

         <cfif checkUser.recordcount GT 0>
           
          <cflocation url="../index.cfm" addtoken="no">
                 
            </cfif>  
 <cfreturn true>
</cffunction>
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 37728901
what is your error?
and are you eliminating the code from your index.cfm page (the one that you are locating to)?
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 333 total points
ID: 37729067
dgrafx is making a great point, if I understand it correctly, he is suggesting that you could end up in an infinite loop here.

what you get to index.cfm page, the same checkUser query will again return 0 records (because nothing has changed) and you will again be redirected to index.cfm page (even though you're already there).

You need to add a check to make sure you don't get redirected to the page you're already on.

<cfif NOT cgi.script_name contains "/index">
    .... now checkUser ...

</cfif>
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 25

Accepted Solution

by:
dgrafx earned 167 total points
ID: 37729139
yes - thats what i meant - thank you ...

and since your target page is named index.cfm and all your directories probably have an index.cfm - you need to either specify exactly like if cgi.script_name is not "/index.cfm"
which means the index.cfm file that is in the root directory but will not match an index.cfm file that is in a folder named xyz as it would be "/xyz/index.cfm".
OR you can rename your "special" page - such as "loginspecial.cfm".
your code would then be if NOT cgi.script_name containsnocase "loginspecial.cfm"
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 333 total points
ID: 37729245
yes, good point.   "contains" is not the best solution there.  I like the idea of using a new file name best.   Even if that file name simply includes the index page (assuming that's the home page perhaps)

loginRequired.cfm ...
   <cfset accessDenied = true>
   <cfinclude template="index.cfm">


Then inside of index.cfm you can test for "accessDenied" and show special messages.  This is one way to reuse the home page but with different messages displayed and also control the redirect from within your application.cfc file..
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 37729317
yup ...

what say you cfbubu?
0
 

Author Closing Comment

by:CFbubu
ID: 37731223
Thanks guys! I was indeed causing an infinite loop....oops!

I wanted to build a way to kick logged-in session users. So I was 'flagging' a row in the user database with a value, than using a query in the onrequest in the application.cfc to check on each page load if the user has been flagged by querying that value. If the user has indeed been flagged, than the user will be directed to a specified page. The issue was that I forgot to destroy the user sessions of a 'flagged' user, and thus this ended up creating the infinite loop error.

I also found the tip about  "<cfif NOT cgi.script_name contains "........"> and the small discussion about it  useful..:)

Also, wanted to extend my well wishes to 'gdemaria' and 'dgrafx' - Hi you guys! Not sure if you remember me. It's been a while since I logged in to get help from this site, but I am happy to see that you are all just as active helping us folks out! You guys did help me answer my questions before. Thanks again for the prompt 'teachings' as always! Much appreciated.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 37731478
Glad it got sorted ...
Have an excellent weekend guys!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question