我正在尝试通过带有 Active Directory 的 LDAP 对 PHP webapp 的用户进行身份验证。
<?php
$ldapconfig['host'] = 'ldapserv.xx.uni.edu';
$ldapconfig['port'] = 389;
$ldapconfig['basedn'] = 'dc=xx, dc=uni, dc=edu';
$ldapconfig['authrealm'] = 'Secure Area';
function ldap_authenticate() {
global $ldapconfig;
global $PHP_AUTH_USER;
global $PHP_AUTH_PW;
if ($PHP_AUTH_USER != "" && $PHP_AUTH_PW != "") {
$ds=@ldap_connect($ldapconfig['host'],$ldapconfig['port']) or exit ("Error connecting to LDAP server.");
//Settings for AD
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$r = @ldap_search( $ds, $ldapconfig['basedn'], 'uid=' . $PHP_AUTH_USER);
if ($r) {
$result = @ldap_get_entries( $ds, $r);
if ($result[0]) {
if (@ldap_bind( $ds, 'uni\\' . $PHP_AUTH_USER, $PHP_AUTH_PW) ) {
return $result[0];
}
}
}
}
header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
header('HTTP/1.0 401 Unauthorized');
return NULL;
}
if (($result = ldap_authenticate()) == NULL) {
echo('Authorization Failed <br />');
exit(0);
}
echo('Authorization success');
print_r($result);
?>
此代码只是不断提示用户输入用户名/密码,除非他们单击取消。我究竟做错了什么?