2015年12月21日 星期一

PHP 查詢 windows AD 帳號範例(取出所有帳號)

<?php
$domain = 'domain';
$ldaprdn = 'userid';
$ldappass = 'password';

$ldapconn = ldap_connect($domain) or die("無法連接至 $domain");

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

if ($ldapconn) { // binding to ldap server
//    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
    $ldapbind = ldap_bind($ldapconn, $ldaprdn . '@' . $domain, $ldappass);
    // verify binding
    if ($ldapbind) {
        $filter = "(sAMAccountName=*)";
        $_dn = str_replace('.', ',dc=', "dc=". $domain);
        $result = ldap_search($ldapconn, $_dn, $filter);

        $info = ldap_get_entries($ldapconn, $result);
        print_r($info);
    } else {
        echo "認證失敗...";
    }
}
?>

沒有留言: