php获取ad域用户:PHP 读取 AD 用户

原创
2016-06-21 08:50:12 1503浏览
  1. //phpinfo();
  2. $host = "******:389";
  3. $user = "**@**";
  4. $pswd = "*****";
  5. $ad = ldap_connect($host) or die( "Could not connect!" );
  6. if($ad){
  7. //设置参数
  8. ldap_set_option ( $ad, LDAP_OPT_PROTOCOL_VERSION, 3 );
  9. ldap_set_option ( $ad, LDAP_OPT_REFERRALS, 0 );
  10. // bool ldap_bind ( resource $link_identifier [, string $bind_rdn = NULL [, string $bind_password = NULL ]] )
  11. $bd = ldap_bind($ad, $user, $pswd) or die ("Could not bind");
  12. echo "ldap_bind success";
  13. //指定需要获取的用户属性
  14. $attrs = array("displayname","cn");
  15. //指定需查询的用户范围
  16. $filter = "(objectclass=*)";
  17. //ldap_search ( resource $link_identifier , string $base_dn , string $filter [, array $attributes [, int $attrsonly [, int $sizelimit [, int $timelimit [, int $deref ]]]]] )
  18. $search = ldap_search($ad, 'DC=**,DC=**,DC=**', $filter, $attrs,0,0,0) or die ("ldap search failed");
  19. $entries = ldap_get_entries($ad, $search);
  20. if ($entries["count"] > 0) {
  21. echo '返回记录数:'.$entries["count"];
  22. for ($i=0; $i$entries["count"]; $i++) {
  23. //所要获取的字段,都必须小写
  24. if(isset($entries[$i]["displayname"])){
  25. echo "

    displayname: "

    .$entries[$i]["displayname"][0]."
    "
    ;//用户名
  26. if(isset($entries[$i]["cn"][0])){
  27. echo "cn: ".$entries[$i]["cn"][0]."
    "
    ;//用户名字
  28. }
  29. }
  30. }
  31. } else {
  32. echo "

    No results found!

    "
    ;
  33. }
  34. }else{
  35. echo "Unable to connect to AD server";
  36. }
  37. ?>
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37245.html



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。