1
我正嘗試使用PHP和LDAP對Active Directory進行身份驗證。不幸的是,當我嘗試通過表單發送用戶名時,它失敗了。即使我在authenticate函數中指定了$ userName和$ passWord,該函數仍會返回false。我究竟做錯了什麼?通過PHP進行LDAP身份驗證仍然失敗
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>
您可以從'ldap_bind()'調用的開頭刪除@,以便查看它是否生成任何錯誤。 – andrewsi
試過了,沒有運氣。 – beta208
你從'ldap_errno()'得到了什麼嗎? – andrewsi