• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

<cfreturn>

After a function is invoked and the return type is boolean.
How can I check for that?

This is what I have:


<cffunction name="get_userLogin" hint="Check if login user exists" returntype="boolean">
      <cfargument name="loginName" type="string" required="true">
      <cfargument name="password" type="string" required="true">
      
      <cfquery datasource="A9968_genuine" name="exists">
        select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
      </cfquery>
      <cfreturn exists>
</cffunction>


<cfinvoke component="components.genuine" method="get_userLogin" argumentcollection="#form#">
      <cfinvokeargument name="loginName" value="#loginName#"/>
      <cfinvokeargument name="password" value="#password#"/>
</cfinvoke>


<!--- If user exists, create a session. --->

<CFIF #exists.recordcount# eq 1>
      <!--- Create Session Structure --->
      <CFSET session.genuine = StructNew()>
      <CFSET session.genuine.fname = #exists.fname#>
      <CFSET session.genuine.lname = #exists.lname#>
      <cflocation url="viewComment.cfm">
<CFELSE>
      <!--- If user does not exist, redirect to login page --->
      <cfset session.genuine.login = "no">
      <cflocation url="genuineLogin.cfm">      
</CFIF>

What I am trying to do is check to see if the query above executed to just one record (someone exists for the loginName and password).

This is the error I get:

Error Occurred While Processing Request  
The value returned from function get_userLogin() is not of type boolean.  
If the component name is specified as a return type, the reason for this error might be that a definition file for such component cannot be found or is not accessible.  
 
0
Isaac
Asked:
Isaac
  • 8
  • 7
  • 4
  • +1
2 Solutions
 
mosphatCommented:
Try this one:
Notice the setting of the session vars occurs in the function as well.

<cffunction name="get_userLogin" hint="Check if login user exists" returntype="boolean">
     <cfargument name="loginName" type="string" required="true">
     <cfargument name="password" type="string" required="true">
     <cfset var exists = 0>
     <cfquery datasource="A9968_genuine" name="exists">
       select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
     </cfquery>
    <cfif exists.recordcount eq 1>
      <CFSET session.genuine = StructNew()>
      <CFSET session.genuine.fname = exists.fname>
      <CFSET session.genuine.lname = exists.lname>
      <cfreturn true>
    <cfelse>
      <cfreturn false>
    </cfif>
</cffunction>
0
 
mosphatCommented:
Some (off-topic) tips:
Notice the <cfset var exists = 0> in my previous post. That is to make sure the variable 'exists' is local to the function. If you don't use the 'var' keyword, you could be overwriting a variable outside the function having the same name. And that is not an easy one to debug. Therefore it is a good habit to always var any variable used within a function.

Second, you don't have to use # within a tag (unless it's in a string).
<CFSET session.genuine.fname = #exists.fname#> should be <CFSET session.genuine.fname = exists.fname>
And <CFIF #exists.recordcount# eq 1> should be <CFIF exists.recordcount eq 1>
Looks a bit cleaner too, I think.

0
 
kkhippleCommented:
why dont you do the check within your component?

<cffunction name="get_userLogin" hint="Check if login user exists" returntype="boolean">
     <cfargument name="loginName" type="string" required="true">
     <cfargument name="password" type="string" required="true">
     
     <cfquery datasource="A9968_genuine" name="exists">
       select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
     </cfquery>

     <!--- if one record is returned, then return true --->
     <CFIF exists.recordcount EQ 1>
         <CFRETURN true>
      <CFELSE>
          <CFRETURN false>
      </CFIF>

</cffunction>


OR .. u can even chg ur query to...

<!---- add and specify "1" to the MAXROWS attribute to the CFQUERY tag --->
     <cfquery datasource="A9968_genuine" name="exists" MAXROWS="1">
       select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
     </cfquery>
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mosphatCommented:
kkhipple,

Using maxrows won't help a lot, since you either find no rows or 1 row.
It is a bit unlikely that there are multiple users having the same loginname and password. ;)

0
 
Tacobell777Commented:
<cffunction name="get_userLogin" hint="Check if login user exists" returntype="boolean">
     <cfargument name="loginName" type="string" required="true">
     <cfargument name="password" type="string" required="true">
     
     <cfquery datasource="A9968_genuine" name="exists">
       select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
     </cfquery>
     <cfreturn exists>
</cffunction>

exists is not boolean, its a query, so if you wanted the above to work you would change  the return type to query, i.e. returntype="query">

Also, I would not put
<CFIF exists.recordcount EQ 1>
         <CFRETURN true>
      <CFELSE>
          <CFRETURN false>
      </CFIF>
in the same function, the function is called get_userLogin, and thats all it should do - you should write another function to which you pass the result from the function and then check if the login is correct.
0
 
Tacobell777Commented:
another thing, I would not actually check if there is a recordcount, I would actually check if the values returned are actually the username and password entered - just some extra security against SQL Injection..
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:

I'm getting a new error now:

Error Occurred While Processing Request  
The value returned from function get_userLogin() is not of type boolean.  
If the component name is specified as a return type, the reason for this error might be that a definition file for such component cannot be found or is not accessible.  
 
What about this.  Can you all help me, I'm just trying to create a login page that only lets you view a few pages.  Maybe my approach is way off.
Anyway Tacobell777, I saw your evocash site, how did you do the login there?  Are there certain steps I need to take.  I'm willing to learn from anybody who know's what they are doing.

-TI-
0
 
mosphatCommented:
The error reads: The value returned from function get_userLogin() is not of type boolean.

That means in your get_userLogin() function there's a <cfreturn xxx> where xxx is not a boolean. Have you correctly changed that function according to the proposed options in this thread?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Yes, I made the appropriate change that you suggested.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
This is what I have.

<cffunction name="get_userLogin" hint="Check if login user exists" returntype="boolean">
      <cfargument name="loginName" type="string" required="true">
      <cfargument name="password" type="string" required="true">
      
      <cfquery datasource="A9968_genuine" name="exists">
        select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
      </cfquery>
      <cfreturn exists>
      
      <!--- If user exists, create a session. --->

<CFIF exists.recordcount eq 1>
      <!--- Create Session Structure --->
      <CFSET session.genuine = StructNew()>
      <CFSET session.genuine.fname = exists.fname>
      <CFSET session.genuine.lname = exists.lname>
      <cflocation url="viewComment.cfm">
<CFELSE>
      <!--- If user does not exist, redirect to login page --->
      <cfset session.genuine.login = "no">
      <cflocation url="genuineLogin.cfm">      
</CFIF>
</cffunction>
0
 
mosphatCommented:
Then there must be a typo in either my or your code...
Could you post what your current version of get_userLogin looks like?
0
 
mosphatCommented:
Hmm, you were quicker :)

You are doing a cflocation now. That means the function is not returning anything. Change returntype="boolean" to returntype="void".

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
With the code below, I get this error:

Error Occurred While Processing Request  
Element RECORDCOUNT is undefined in EXISTS.  

I made the returntype=query

<cffunction name="get_userLogin" hint="Check if login user exists" returntype="query">
     <cfargument name="loginName" type="string" required="true">
     <cfargument name="password" type="string" required="true">
     
     <cfquery datasource="A9968_genuine" name="exists">
       select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
     </cfquery>
     <cfreturn exists>
     
     <!--- If user exists, create a session. --->

</cffunction>

"INVOKE PAGE"

<cfinvoke component="components.genuine" method="get_userLogin" argumentcollection="#form#" returnvariable="exist">
      <cfinvokeargument name="loginName" value="#loginName#"/>
      <cfinvokeargument name="password" value="#password#"/>
</cfinvoke>


<!--- If user exists, create a session. --->

<CFIF #exists.recordcount# eq 1>
      <!--- Create Session Structure --->
      <CFSET session.genuine = StructNew()>
      <CFSET session.genuine.fname = #exists.fname#>
      <CFSET session.genuine.lname = #exists.lname#>
      <cflocation url="viewComment.cfm">
<CFELSE>
      <!--- If user does not exist, redirect to login page --->
      <cfset session.genuine.login = "no">
      <cflocation url="genuineLogin.cfm">      
</CFIF>


 
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
with the void, I get this error:

Error Occurred While Processing Request  
The function get_userLogin defined as void tried to return a value.  
Void functions must not return any values.  
 
0
 
kkhippleCommented:
in the code below.. if this is waht you are using.... dont specify the CFLOCATION ... instead just return TRUE or FALSE

<cffunction name="get_userLogin" hint="Check if login user exists" returntype="boolean">
     <cfargument name="loginName" type="string" required="true">
     <cfargument name="password" type="string" required="true">
     
     <cfquery datasource="A9968_genuine" name="exists">
       select * from tblgenuine_login
      where login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
      and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
     </cfquery>
     <cfreturn exists>
     
     <!--- If user exists, create a session. --->

<CFIF exists.recordcount eq 1>
     <!--- Create Session Structure --->
     <CFSET session.genuine = StructNew()>
     <CFSET session.genuine.fname = exists.fname>
     <CFSET session.genuine.lname = exists.lname>
     <cflocation url="viewComment.cfm">
<CFELSE>
     <!--- If user does not exist, redirect to login page --->
     <cfset session.genuine.login = "no">
     <cflocation url="genuineLogin.cfm">    
</CFIF>
</cffunction>
0
 
kkhippleCommented:
and in this bit of code..

<CFIF exists.recordcount eq 1>
     <!--- Create Session Structure --->
     <CFSET session.genuine = StructNew()>
     <CFSET session.genuine.fname = exists.fname>
     <CFSET session.genuine.lname = exists.lname>
     <cflocation url="viewComment.cfm">
<CFELSE>
     <!--- If user does not exist, redirect to login page --->
     <cfset session.genuine.login = "no">
     <cflocation url="genuineLogin.cfm">    
</CFIF>
</cffunction>



change it to

<CFIF exists.recordcount eq 1>
     <CFRETURN true>
<CFELSE>
     <CFRETURN FALSE>
</CFIF>
</cffunction>





after that... set your session variables and what not
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
What do you mean kkhipple?

In replace of this '<cflocation url="viewComment.cfm">', I do this '<cfreturn true>'
Is that what you mean?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
kkhipple,

If I do it the way you said above with the true or false, how would it know where to redirect it, because the cflocation is not there anymore.
0
 
mosphatCommented:
The cflocation should be in the code calling that function:

<cfset genuineCFC = createObject("component", "components.genuine")>
<cfif genuineCFC.get_userLogin()>
  <cflocation url="viewComment.cfm">
<cfelse>
  <cflocation url="genuineLogin.cfm">
</cfif>
0
 
mosphatCommented:
Make that:

<cfset genuineCFC = createObject("component", "components.genuine")>
<cfif genuineCFC.get_userLogin(form.loginName, form.password)>
  <cflocation url="viewComment.cfm">
<cfelse>
  <cflocation url="genuineLogin.cfm">
</cfif>
0
 
kkhippleCommented:
that's exactly how you do it... ... thx mosphat...
get_userLogin ...  should only authenticate the user .. from my understanding of what you wanna do.
if u want.. u can rename it ... authenticateUser .. or validUser... just something that speaks more clearly  about what  you are trying to do...


i hope the help that mosphat has provided was beneficial...
0
 
Tacobell777Commented:
Forget evocash, its history ;-)

<cfcomponent>

      <cffunction
            name="getUser"
            hint="Returns a user"
            returntype="query">
      
            <cfargument
                  name="loginName"
                  type="string"
                  required="true" />
            <cfargument
                  name="password"
                  type="string"
                  required="true" />
      
            <cfquery
                  datasource="A9968_genuine"
                  name="qUser">
            SELECT            login_name,
                              password      
            FROM            tblgenuine_login
            WHERE            login_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.logiNname#"/>
            AND                  password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
            </cfquery>
      
            <cfreturn qUser />
      
      </cffunction>

   
      <cffunction
            name="isValidUser"
            hint="Checks the login details"
            returntype="boolean">

            <cfargument
                  name="loginName"
                  type="string"
                  required="true" />
            <cfargument
                  name="password"
                  type="string"
                  required="true" />
      
            <cfscript>
            var result getUser( loginName = arguments.loginName, password = arguments.password );
            if ( result.login_name IS arguments.loginName AND result.password IS arguments.password )
            {
                  return true;
            }
            else
            {
                  return false;
            }
            </cfscript>

      </cffunction>

</cfcomponent>

<cfscript>
obj = createObject( "component", "whateverthename" );
</cfscript>

<cfif obj.isValidUser( loginName = 'xxxx', password = 'xxxx' ) >
      <cflocation url="do the relocation here because its a valid user" />
<cfelse>
      Bad login
</cfif>
0
 
Tacobell777Commented:
just a note: if you really wanted it OO then you would probably get the user from the db, set it in the this scope, create a getter and then do isValidUser() getting the values from the THIS scope (you would probably go further than that, but I won't go there ;-)
but the above is broken down more then before anyway, the whole idea of CFCs is that you allow a function to perform only one action, this way you can create loosely coupled code.

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 8
  • 7
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now