本文介绍了如果Else查询为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CFLDAP让用户使用活动目录进行身份验证。我试图写一个if语句,以防用户信息不回来认证。我想我可以使用< cfif AuthenticateUser.RecordCount gt 0> ,这是工作,只要信息是正确的,但如果输入错误的信息,没有验证它没有运行else语句。

I am using CFLDAP to have users get authenticated using active directory. I am trying to write an if statement in case the users information does not come back as authenticated. I thought I could check by using <cfif AuthenticateUser.RecordCount gt 0> which is working as long as the information is correct but if the wrong information is entered and nothing is authenticated it is not running the else statement. Any help with this would be greatly appreciated!

<cfldap action="query"
              name="AuthenticateUser"
              attributes="dn,mail,givenname,sn,samaccountname,memberof"
              start="DC=domain,DC=net"
              filter="(&(objectclass=user)(samAccountName=#trim(form.user_name)#))"
              server="servername"
              Port="389"
              username="tc\#trim(form.user_name)#"
              password="#trim(form.user_pass)#">
<cfoutput>#AuthenticateUser.RecordCount#</cfoutput>
<!--- Get all records from the database that match this users credentials --->
<cfquery name="userVerify" datasource="test">
    SELECT  *
    FROM    dbo.Users
    WHERE   user_name = <cfqueryparam value="#AuthenticateUser.samaccountname#" cfsqltype="cf_sql_varchar" />
</cfquery>
<cfif AuthenticateUser.RecordCount gt 0>
    <!--- This user has logged in correctly, change the value of the session.allowin value --->
    <cfset session.allowin = "True" />
    <cfset session.employee_number = userVerify.employee_number />

     <!--- Now welcome user and redirect to "index.html" --->
    <script>
        self.location="../dashboard/dashboard.cfm";
    </script>
<cfelse>
    <!--- this user did not log in correctly, alert and redirect to the login page --->
    <script>
        alert("Your credentials could not be verified, please try again!");
        self.location="Javascript:history.go(-1)";
    </script>
    </cfif>



我也尝试过:< cfif len(AuthenticateUser)> / code>

I have also tried: <cfif len(AuthenticateUser)>

推荐答案

我认为当查询失败时抛出错误。请尝试以下操作:

I think it throws an error when the query fails. Try this:

<cftry>
    <cfldap  action="query"
              name="AuthenticateUser"
              attributes="dn,mail,givenname,sn,samaccountname,memberof"
              start="DC=domain,DC=net"
              filter="(&(objectclass=user)(samAccountName=#trim(form.user_name)#))"
              server="servername"
              Port="389"
              username="tc\#trim(form.user_name)#"
              password="#trim(form.user_pass)#">
   <cfset LoginStatus = "Success">
   <cfcatch type="any">
       <cfset LoginStatus = "Failed">
   </cfcatch>
</cftry>

然后您的cfif将是这样:

Then your cfif would be something like this:

<cfif LoginStatus eq "Success">
    <!--- This user has logged in correctly, change the value of the session.allowin value --->
    <cfset session.allowin = "True" />
    <cfset session.employee_number = userVerify.employee_number />

     <!--- Now welcome user and redirect to "index.html" --->
    <script>
        self.location="../dashboard/dashboard.cfm";
    </script>
<cfelse>
    <!--- this user did not log in correctly, alert and redirect to the login page --->
    <script>
        alert("Your credentials could not be verified, please try again!");
        self.location="Javascript:history.go(-1)";
    </script>
</cfif>

我认为这适用于CF9。

I think this works on CF9.

这篇关于如果Else查询为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 07:36