Php 如何在基本身份验证中显示错误消息?
我使用PHP实现了如下基本身份验证:Php 如何在基本身份验证中显示错误消息?,php,authentication,basic-authentication,Php,Authentication,Basic Authentication,我使用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"'); $UsrI
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();
}
if((isset($服务器['PHP\u AUTH\u用户])和&$服务器['PHP\u AUTH\u用户]]='')(isset($服务器['PHP\u AUTH\u PW'])和&$服务器['PHP\u AUTH\u PW']=''){
标题('WWW-Authenticate:Basic realm=“authentication”');
$UsrId=$objLDAP->authenticateUser();
模具();
}elseif(isset($_服务器['PHP\u AUTH\u用户'])和&isset($_服务器['PHP\u AUTH\u PW'])){
$UsrId=$objLDAP->authenticateUser($\u服务器['PHP\u AUTH\u用户'],$\u服务器['PHP\u AUTH\u PW']);
}否则{
标题('WWW-Authenticate:Basic realm=“我的领域”);
标题(“HTTP/1.0 401未经授权”);
回显“window.location.reload();”;
//$UsrId=$objLDAP->authenticateUser();
//$smarty->display($templates['budWithoutAnyAccess']);
模具();
}
如果($pUser){
//编码
}否则{
标题(“需要HTTP/1.1 401授权”);
标头('WWW-Authenticate:Basic realm=“拒绝访问”);
$UsrId=$objLDAP->authenticateUser();
}
它会打开登录弹出窗口
如果用户提供了错误的凭据,我可以在页面上显示错误消息,但在“重新刷新”页面上,它也应该打开“身份验证登录”弹出窗口
在取消时,我想显示消息,在刷新时,它应该打开身份验证登录弹出窗口
我该怎么做
谢谢,,
Trupti请检查这个
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;
}
}
if(!isset($\u SERVER['PHP\u AUTH\u USER']))
{
标题('WWW-Authenticate:Basic realm=“Sally Port”);
标题(“HTTP/1.0 401未经授权”);
echo“单击以重新加载”;
出口
}
if(检查ldapuser($ldapServername))
{
//如果ldap authentican成功,则将用户重定向到gateway.php
标题(“位置:gateway.php”);
}
其他的
{
//清除全局变量。
取消设置($_服务器['PHP_AUTH_用户]);
取消设置($_服务器['PHP_AUTH_PW']);
//如果密码不正确,则显示弹出窗口,直到密码正确。
while(checkLDAPUser($ldapServername)!=1)
{
取消设置($_服务器['PHP_AUTH_用户]);
取消设置($_服务器['PHP_AUTH_PW']);
标题('WWW-Authenticate:Basic realm=“Sally Port”);
标题(“HTTP/1.0 401未经授权”);
echo“单击以重新加载”;
出口
}
}
函数checkLDAPUser($ldapServername)
{
$username=$\u服务器['PHP\u AUTH\u USER'];
$password=$_服务器['PHP_AUTH_PW'];
//$adServer=“ldap.”.$ldapServername.“.com”;
$adServer=”ldap://bchq-dc-v1.blackcreek.local";
$ldap=ldap\u connect($adServer);
如果($ldap)
{WriteLog(“LDAP连接”);
}
其他的
{WriteLog(“LDAP失败”);
}
$ldaprdn=“cn=只读管理员,dc=示例,dc=com”;
ldap_设置_选项($ldap,ldap_选项_协议_版本,3);
ldap\u set\u选项($ldap,ldap\u OPT\u references,0);
$bind=@ldap\u bind($ldap,$ldaprdn,$password);
//如果连接成功,请设置会话。
如果($bind)
{
返回1;
}
其他的
{console.log(“LDAP用户名和密码不正确”);
返回0;
}
}
您是否查看了示例并进行了尝试?试试这个@是的。我试过那个例子。点击登录按钮后,不会打开登录弹出窗口。我想让它打开重新刷新页面上的弹出窗口。@sumant,如果用户单击“取消”或提供了错误的凭据,我想打开登录弹出窗口。但是您的代码比上面的代码多,对吗?请给我们看看你有什么。仅从上面两个标题很难知道发生了什么。这到底是怎么回事?如果没有传递用户名,它只会创建一个无限循环(就像第一个请求一样)。$\u服务器['PHP\u AUTH\u USER']
无法在该循环中更改其值。是的…它只继续加载…似乎进入无限循环…@Trupti请查看我更新的ans是否有帮助?@MagnusEriksson请检查它