欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-04-15 15:19:00
...
  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