廣告贊助

AD 的部份

<?php
// connect to AD server
$ldapconn = ldap_connect("AD主機") or die("Could not connect to AD server.");
$set = ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);

$ldap_bd = ldap_bind($ldapconn,"Administrator@AD主機","密碼");
$result = ldap_search($ldapconn,"ou=employee,dc=ad,dc=bojack,dc=com,dc=tw","(CN=bojack)") or die ("Error in query");

$data = ldap_get_entries($ldapconn,$result);

echo $data["count"]. " entries returned\n";

for($i=0; $i<=$data["count"];$i++) {
        for ($j=0;$j<=$data[$i]["count"];$j++) {
                echo $data[$i][$j].": ".$data[$i][$data[$i][$j]][0]."\n<br>";
        }
}
ldap_close($ldapconn);
?>

LDAP 的部份

<?php
// connect to ldap server
$ldapconn = ldap_connect("LDAP 主機")  or die("Could not connect to LDAP server.");
$set = ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);

$ldap_bd = ldap_bind($ldapconn,"cn=root,dc=ldap,dc=bojack,dc=com,dc=tw","密碼");
$result = ldap_search($ldapconn,"ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw","(uid=bojack)") or die ("Error in query");

$data = ldap_get_entries($ldapconn,$result);

echo $data["count"]. " entries returned\n";

for($i=0; $i<=$data["count"];$i++) {
        for ($j=0;$j<=$data[$i]["count"];$j++) {
                echo $data[$i][$j].": ".$data[$i][$data[$i][$j]][0]."\n<br>";
        }
}

/* 若要 show 出某個欄位 */
echo $data[0]["userpassword"][0] ."<br>";

ldap_close($ldapconn);
?>

若要比對密碼是否正確 (以LDAP為例)

<?php
if ($ldapconn) {

if(@ldap_bind($ldapconn,"uid=bojack,ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw","密碼"))
{
        echo "password correct!";
} else {
        echo "wrong password!";
}

}

?>

參考資料

Integrating Active Directory with PHP

PHP程式使用 Windows Server AD 認證身份

Modifying Active Directory passwords through PHP and IIS ( 這一篇有說怎麼改 AD 的密碼,但我試不出來 -_- )

PHP/LDAP - change user password in ad

創作者介紹

有為青年生活札記

bojack 發表在 痞客邦 PIXNET 留言(0) 人氣()