- <?php
- //phpinfo();
- $host = "******:389";
- $user = "**@**";
- $pswd = "*****";
- $ad = ldap_connect($host) or die( "Could not connect!" );
- if($ad){
- //設定參數
- ldap_set_option ( $ad, LDAP_OPT_PROTOCOL_VERSION, 3 );
- ldap_set_option ( $ad, LDAP_OPT_REFERRALS, 0 );
- // bool ldap_bind ( resource $link_identifier [, string $bind_rdn = NULL [, string $bind_password = NULL ]] )
- $bd = ldap_bind($ad, $user, $pswd) or die ("Could not bind");
- echo "ldap_bind success";
- //指定需要擷取的使用者屬性
- $attrs = array("displayname","cn");
- //指定需查詢的使用者範圍
- $filter = "(objectclass=*)";
- //ldap_search ( resource $link_identifier , string $base_dn , string $filter [, array $attributes [, int $attrsonly [, int $sizelimit [, int $timelimit [, int $deref ]]]]] )
- $search = ldap_search($ad, 'DC=**,DC=**,DC=**', $filter, $attrs,0,0,0) or die ("ldap search failed");
- $entries = ldap_get_entries($ad, $search);
- if ($entries["count"] > 0) {
- echo '返回記錄數:'.$entries["count"];
- for ($i=0; $i<$entries["count"]; $i++) {
- //所要擷取的欄位,都必須小寫
- if(isset($entries[$i]["displayname"])){
- echo "<p>displayname: ".$entries[$i]["displayname"][0]."<br />";//使用者名稱
- if(isset($entries[$i]["cn"][0])){
- echo "cn: ".$entries[$i]["cn"][0]."<br />";//使用者名稱字
- }
- }
- }
- } else {
- echo "<p>No results found!</p>";
- }
- }else{
- echo "Unable to connect to AD server";
- }
- ?>
本文連結http://www.cxybl.com/html/wlbc/Php/20130319/37245.html