【问题标题】:how to show error message in basic authentication?如何在基本身份验证中显示错误消息?
【发布时间】:2020-06-03 15:12:21
【问题描述】:

使用 PHP,我实现了基本身份验证,如下所示:

if ((isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER']=='') || (isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_PW']=='')) {
    header('WWW-Authenticate: Basic realm="Authentification"');
    $UsrId = $objLDAP->authenticateUser();
    die();
} elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
    $UsrId = $objLDAP->authenticateUser($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
}else{ 
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo '<script>window.location.reload();</script>';
    //$UsrId = $objLDAP->authenticateUser();
    //$smarty->display($templates['budWithoutAnyAccess']);
    die();
}
if ($pUser){
 //coding
}else{
    header('HTTP/1.1 401 Authorization Required');
    header('WWW-Authenticate: Basic realm="Access denied"');
    $UsrId = $objLDAP->authenticateUser();
}

它会打开登录弹出窗口。

如果用户提供了错误的凭据,我可以在页面上显示错误消息,但在页面重新刷新时它也应该打开身份验证登录弹出窗口。

在取消时,我想显示消息并在刷新时,它应该打开身份验证登录弹出窗口。

我该怎么做?

谢谢你, 特鲁提

【问题讨论】:

  • 您检查过the manual 中的示例并尝试过吗?
  • @MagnusEriksson 是的。我试过那个例子。点击登录按钮后,它不会打开登录弹出窗口。我希望它在页面重新刷新时打开弹出窗口。
  • @sumant,如果用户单击取消或提供错误凭据,我想打开登录弹出窗口。
  • 但是你有比上面更多的代码,对吧?请告诉我们你有什么。仅从以上两个标头很难知道发生了什么。

标签: php authentication basic-authentication


【解决方案1】:

请检查这个!

 if (!isset($_SERVER['PHP_AUTH_USER']))
    {
        header('WWW-Authenticate: Basic realm="Sally Port"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Click <a href="login.php">here</a> to reload';
        exit;
    }

    if(checkLDAPUser($ldapServername))
    {
        // If ldap authentican is successful then redirect the user to gateway.php
        header("location:gateway.php");
    }
    else
    {
        //Clear global variables.
        unset($_SERVER['PHP_AUTH_USER']);
        unset($_SERVER['PHP_AUTH_PW']);

    //  If the password is incorrect, show popup until the password is correct.
        while(checkLDAPUser($ldapServername)!=1)
        {
            unset($_SERVER['PHP_AUTH_USER']);
            unset($_SERVER['PHP_AUTH_PW']);
            header('WWW-Authenticate: Basic realm="Sally Port"');
            header('HTTP/1.0 401 Unauthorized');
            echo 'Click <a href="login.php">here</a> to reload';
            exit;
        }
    }
function checkLDAPUser($ldapServername)
{
    $username=$_SERVER['PHP_AUTH_USER'];
    $password=$_SERVER['PHP_AUTH_PW'];

    //$adServer = "ldap.".$ldapServername.".com";
    $adServer = "ldap://bchq-dc-v1.blackcreek.local";
    $ldap = ldap_connect($adServer);
if($ldap)
{WriteLog("LDAP connected");

}
else
{WriteLog("LDAP Failed");

}
    $ldaprdn = "cn=read-only-admin,dc=example,dc=com";
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
    $bind = @ldap_bind($ldap, $ldaprdn, $password);             
    //Set up session if connect is successful.
    if($bind)
    {
        return 1;
    }
    else
    {console.log("LDAP Username and Passwords are incorrect");

        return 0;
    }
}

【讨论】:

  • 这到底是如何工作的?如果没有传递用户名(就像对它的第一个请求),它只会创建一个无限循环。 $_SERVER['PHP_AUTH_USER'] 无法在该循环中更改其值。
  • 是的...它只保持加载...似乎进入无限循环...
  • @Trupti 请看看我更新的答案有帮助吗?
  • @MagnusEriksson 请检查一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2012-08-15
  • 2020-08-22
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多