1

ユーザーと管理者が同じフォームを使用してログインできる Classic ASP を使用して作成した単純なログイン フォームです。

データベースの「Users」テーブルで、「status」というフィールドを作成しました。DATA TYPE は BIT (つまり、「0」または「1」) の値が受け入れられます。新規登録ユーザーごとのデフォルト値は「0」です。

ステータスが「0」の場合、特定のユーザーまたは管理者が Authentication.asp ページにリダイレクトされ、そこでいくつかの質問に答え、SUBMIT をクリックすると、データベースのステータス値が「1」に設定されるクエリを渡しました。 . "1" は、ユーザーまたは管理者が認証フォームに入力し、Authentication.asp ページにリダイレクトするのではなく、目的のページにリダイレクトできることを示します。そのために、If-elseif-else ステートメントを使用しました。何度も再確認しましたが、if-else ステートメントに欠けているものはないようです。しかし、データベースのステータス列のステータスとして「1」を手動で入力しても、常に認証ページにリダイレクトされます。これが私のコードです。

Session("Username")=request.form("user_name")
    if request.Form("sub_but") <> "" then
    sql = "SELECT * FROM Users WHERE UserName='"&request.form("user_name")&"' AND Password='"&request.form("pwd")&"'"
    rs.open sql, con, 1, 2  
    if rs.EOF then
            response.Write("<script language='javascript'>{attention_emp();}</script>")
        else
        if(rs("status")=1 & rs("login_type")="admin") then
                  Response.Redirect ("admin.asp")
        elseif(rs("status")=1 & rs("login_type")="emp") then
                      response.Redirect("leave.asp")
        else
                      response.Redirect("auth.asp") 
        end if  
    end if

    rs.close
    end if 
4

1 に答える 1

4

Do not use "&" use the following:

if(rs("status")=1 AND LCase(rs("login_type"))="admin") then
              Response.Redirect ("admin.asp")
    elseif(rs("status")=1 AND LCase(rs("login_type"))="emp") then
                  response.Redirect("leave.asp")
    else
                  response.Redirect("auth.asp") 
    end if  

In vbScript "&" is a string concatenation operator not a logical operator. Here is a list of vbScript operators.

EDIT also note that string comparisons are case sensitive. I've changed the comparison to convert the database output to lower case. This could also be a factor in your problem.

Also take note of @Mirko comment!!!

EDIT 2 To Clarify from comments

If you are still always getting redirected to the authentication page the actual cause needs to be determined. Hard coding the conditional statement should help rule it out as being the cause. Another method for debugging is to pull apart the statement instead of redirecting. See below:

if(rs("status")=1 AND LCase(rs("login_type"))="admin") then
    Response.Redirect ("admin.asp")
elseif(rs("status")=1 AND LCase(rs("login_type"))="emp") then
    response.Redirect("leave.asp")
else
    Response.Write "Status: " & rs("status") & "<br />"
    Response.Write "Is Status 1: " & (rs("status")=1)  & "<br />"
    Response.Write "Login Type: " & rs("login_type") & "<br />"
    Response.Write "Is Login Type admin: " & (rs("login_type") = "admin") & "<br />"
    Response.Write "Is Login Type emp: " & (rs("login_type") = "emp") & "<br />"
end if  

This will help you determine what is true and what is false in you if and if/else statements. Once you have done that is should become clearer as to what is causing the behavior you are getting. Once you have isolated and fixed the problem, put back the redirect.

于 2012-05-11T04:14:12.917 に答える