我正在尝试使用 PHP 和 LDAP 对 Active Directory 进行身份验证。不幸的是,当我尝试通过表单发送用户名时,它失败了。即使我在 authenticate 函数中指定了 $userName 和 $passWord,该函数仍然返回 false。我究竟做错了什么?
PHP:
<?php
session_start();
function authenticate($userName,$passWord) {
//Active Directory Server
$ldap_host = "example.com";
//Active Directory DN
$ldap_dn = "DC=myCompany,DC=org";
//Connect to AD Server
$ldap = ldap_connect($ldap_host)
or die("Could not connect to LDAP server.");
utf8_decode($passWord);
//Specify Active Directory
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
//Verify userName and passWord
if($bind = @ldap_bind($ldap, $userName, $passWord)) {
//if valid
$ldapResults = 'Bind Successful';
return true;
} else {
//invalid username or password
return false;
}
};
if(isset($_POST['userName'])) {
//authenticate via function
if(authenticate($_POST['userName'],$_POST['passWord']))
{
//successfully authenticated (scroll down)
header("Location: example.com");
$error = null;
} else {
//failed authentication
$error = 1;
}
}
?>
HTML:
<form method="post" id="loginForm" action="login.php">
<ul>
<li>
<input type="text" id="userName" name="userName" />
</li>
<li>
<input type="password" id="passWord" name="passWord" />
</li>
<li>
<input type="submit" name="submit" value="Log In"></input>
</li>
</ul>
</form>