cflocation problem

Hello experts
I want to use cflocation in my application.cfm to redirect not logged in users to the login.cfm page
but i get a browser error that the page is not redirecting right and that the load process has stopped(free translation).
the code:
<cfif session.allowin neq "true">
<cflocation url="login.cfm">
</cfif>

Using the next code it is working:
<cfif session.allowin neq "true">
      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">
      <cfelse>
      <!--- this user is not logged in, alert user and redirect to the login.cfm page --->
      <script>
           
              self.location="login.cfm";
      </script>
      </cfif>
</cfif>

I want to use the first code.
Any helpto fix it?
LVL 2
PanosAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
<cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">

Open in new window


Oh dear... If captchaImage.cfm is in the same directory as your Application.cfm, that won't work.  captchaImage.cfm is a CF page too ;-) so every time the login page gets a new image, CF will run the Application.cfm ... again.

It will see the user is NOT logged in:
           <cfif session.allowin neq "true">
   
... the page requested ("captchaImage.cfm") is NOT "login.cfm"

      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">

... so it will redirect to Login.cfm (or possibly create an infinite loop).

          <cflocation url="login.cfm">

Either way, the captcha image never loads!  To fix it, either exclude the captchaImage.cfm page too, or move it outside the Application.cfm directory.

ie
<cfif session.allowin neq "true">
    <cfif NOT ListFindNoCase("login.cfm,captchaImage.cfm", ListLast(CGI.SCRIPT_NAME, "/"))>
	    <cflocation url="login.cfm" addtoken="false">
    </cfif>
</cfif>

Open in new window


Totally unrelated ...

<cfset this.secureJSON = "false">

Open in new window


The "this" scope only applies to Application.cfc files, not .cfm.  Also the session variable cfparam's should come after the cfapplication tag.  So you might want review the code. Tighten it up, and maybe switch to an Application.cfc too.

0
 
Bhavesh ShahConnect With a Mentor Lead AnalysistCommented:
Hi,

you must be tried it still, did u check this.

<cfif session.allowin neq "true">
      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">
      <cfelse>
     <cflocation url="login.cfm">
      </cfif>
</cfif>
0
 
PanosAuthor Commented:
Hi Brichsoft.
Sometimes the solution is in front of me and i don't see it.
The redirecting problem has been solved and a new one did come up.
I have a captcha image (<img src="#request.AdminPath#captcha/captchaImage.cfm?r=<cfoutput>#gettickcount()#</cfoutput>" id="sessioncaptcha" alt="captcha" width="190" height="60">)
on the login page that is not loading any more.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Bhavesh ShahLead AnalysistCommented:
hi,

can u share us the code??
0
 
PanosAuthor Commented:
Check this:
<!--- application part --->
<cfscript>
SetEncoding("form","utf-8");
SetEncoding("url","utf-8");
</cfscript>
<cfsetting enablecfoutputonly="true">
<cfcontent type="text/html; charset=utf-8">
<cfset application.vlangid = 3 >
<cfset this.secureJSON = "false">
<cfset request.AdminPath = 'http://mydomain/admin/'>
<cfset request.homeURL = "http://mydomain/">
<cfset request.dsn = "carbay">
<CFPARAM NAME="session.user_id"  type="numeric" DEFAULT="0">

<!--- Now define that this user is logged out by default --->
<CFPARAM NAME="session.allowin" DEFAULT="false">

<CFPARAM NAME="session.clientlocked"  type="numeric" DEFAULT="0">

<cfapplication clientmanagement="Yes"
               sessiontimeout="#CreateTimeSpan(0,0,30,0)#"
               applicationtimeout="#CreateTimeSpan(0,2,0,0)#"
               sessionmanagement="yes"
               setclientcookies="yes"/>
               
<cfif session.allowin neq "true">
      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">
      <cfelse>
     <cflocation url="login.cfm">
      </cfif>
</cfif>



<!--- login part --->
<div style="float:left; display:inline; text-align:left; width:194px;">Reload Captcha
  <span style="display:inline-block"><img src="#request.AdminPath#admincaptcha/captchaImage.cfm?r=<cfoutput>#gettickcount()#</cfoutput>" id="sessioncaptcha" alt="captcha" width="190" height="60"></span><button type="button" name="btnRefreshCaptcha" class="login_button" value="#getLang.Text#" onClick="document.getElementById('sessioncaptcha').src='#request.AdminPath#admincaptcha/captchaImage.cfm?r='+new Date().getTime();"/>Reload Captcha</button></div>
 
 
<!--- captchaImage.cfm --->
<cfscript>
function getRandomCaptchaText(){
  var rStr = "";
        var i = 0;
  rStr = rStr & Chr(RandRange(Asc("A"),Asc("Z")));
  rStr = rStr & Chr(RandRange(Asc("A"),Asc("Z")));
  for(i=0;i<3;i=i+1){
    rStr = rStr & Chr(RandRange(Asc("0"),Asc("9")));
  };
  return rStr;
}
</cfscript>
<cfset Session.captcha_adminText = getRandomCaptchaText()/>
<cfset captchaimagepath = getdirectoryfrompath(getcurrenttemplatepath()) & 'admincaptcha' & gettickcount() & '.png'>
<cfimage action="captcha" width="192" height="60" text="#session.captcha_adminText#" destination="#captchaimagepath#" difficulty="medium">
<cfcontent reset="yes" type="image/png" file="#captchaimagepath#" deletefile="yes">
0
 
Bhavesh ShahLead AnalysistCommented:
Hi,

Try this

<cfoutput>

<img src="#request.AdminPath#captcha/captchaImage.cfm?r=#gettickcount()#" id="sessioncaptcha" alt="captcha" width="190" height="60">

</cfoutput>
0
 
PanosAuthor Commented:
The problem remains.
0
 
PanosAuthor Commented:
i set one output in the login page to check the session output:
#Session.captcha_adminText#
When i click on the button to change the session value and than reload the page nothing happens.
This means that something is wrong with the path setting when i use cflocation.
Checking the source code everything is OK.
0
 
Bhavesh ShahLead AnalysistCommented:
Hi,

Show your code closely.

<img src="#request.AdminPath#admincaptcha/captchaImage.cfm?r=<cfoutput>#gettickcount()#</cfoutput>" id="sessioncaptcha" alt="captcha" width="190" height="60">

I dont think,this can be done.

you doing first time???

I think you need to use ajax....to show image
0
 
PanosAuthor Commented:
This code in application has the same result:
<cfif session.allowin neq "true">
<cfinclude template="login.cfm">
<cfabort>
</cfif>
0
 
PanosAuthor Commented:
It is the first time i use the code in the admin/application.cfm page because i want to protect the admin folder.
0
 
PanosAuthor Commented:
Can you suggest another tutorial using ajax with the possibility to change tha captcha text?
0
 
Bhavesh ShahLead AnalysistCommented:
Hi,

If its already given by expert and worked once then you no need to change it.
we will find the problem and then solved....anyways I'm sure once agx show this Question,you will surely get answer soon ;-)

anyways finally where we stuck....can u say again???PLS
0
 
PanosAuthor Commented:
The code is working(captcha image is showing and the button is changing the session value) only if i use the javascript way  in application.cfm
<cfif session.allowin neq "true">
      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">
      <cfelse>
      <!--- this user is not logged in, alert user and redirect to the login.cfm page --->
      <script>
           
              self.location="login.cfm";
      </script>
      </cfif>
</cfif>

Not the cflocation either the cfinclude.
0
 
Bhavesh ShahLead AnalysistCommented:
Hi,

I understud the problem....and i know this happens with cflocation but not able to say exact reason.....

I will get back to you soon....
0
 
Bhavesh ShahLead AnalysistCommented:

Agx sir,

You late by one day. ;-)

I was expecting your reply yesterday........
0
 
_agx_Commented:
lol.  I didn't notice the thread was stalled until today ;-)
0
 
PanosAuthor Commented:
!!!!WOW THATS IT !!!
Thank you very much agx for the solution and for the advices to my application.cfm page.


(like Brichsoft allready said:You late by one day)

0
 
PanosAuthor Commented:
thank you again.
Some points are given to expert Brichsoft for his interest.
regards
panos
0
 
_agx_Commented:
>> You late by one day

lol.  Well we've all got jobs outside of EE ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.