如何使用PHP从LDAP目录获取用户列表?
发布时间:2020-12-13 18:22:46 所属栏目:PHP教程 来源:网络整理
导读:$ldaphost = "my_host_name";$ds=ldap_connect($ldaphost) or die("Could not connect to $ldaphost"); ldap_set_option ($ds,LDAP_OPT_REFERRALS,0);ldap_set_option ($ds,LDAP_OPT_PROTOCOL_VERSION,3);if ($ds) { $basedn = 'my_dc_string'; $samaccountn
$ldaphost = "my_host_name"; $ds=ldap_connect($ldaphost) or die("Could not connect to $ldaphost"); ldap_set_option ($ds,LDAP_OPT_REFERRALS,0); ldap_set_option ($ds,LDAP_OPT_PROTOCOL_VERSION,3); if ($ds) { $basedn = 'my_dc_string'; $samaccountname = 'my_user_name'; $filters = "(samaccountname={$samaccountname})"; $result = ldap_search($ds,$basedn,$filters); } 如何使用PHP从LDAP获取所有用户的列表?上面的代码在ldap_search函数上失败,发出此警告 “警告:ldap_search():搜索:操作错误” 我的用户名,ldaphost等是正确的.我不确定过滤器. /** * Get a list of users from Active Directory. */ $ldap_password = 'PASSWORD'; $ldap_username = 'USERNAME@DOMAIN'; $ldap_connection = ldap_connect(HOSTNAME); if (FALSE === $ldap_connection){ // Uh-oh,something is wrong... } // We have to set this option for the version of Active Directory we are using. ldap_set_option($ldap_connection,3) or die('Unable to set LDAP protocol version'); ldap_set_option($ldap_connection,0); // We need this for doing an LDAP search. if (TRUE === ldap_bind($ldap_connection,$ldap_username,$ldap_password)){ $ldap_base_dn = 'DC=XXXX,DC=XXXX'; $search_filter = '(&(objectCategory=person)(samaccountname=*))'; $attributes = array(); $attributes[] = 'givenname'; $attributes[] = 'mail'; $attributes[] = 'samaccountname'; $attributes[] = 'sn'; $result = ldap_search($ldap_connection,$ldap_base_dn,$search_filter,$attributes); if (FALSE !== $result){ $entries = ldap_get_entries($ldap_connection,$result); for ($x=0; $x<$entries['count']; $x++){ if (!empty($entries[$x]['givenname'][0]) && !empty($entries[$x]['mail'][0]) && !empty($entries[$x]['samaccountname'][0]) && !empty($entries[$x]['sn'][0]) && 'Shop' !== $entries[$x]['sn'][0] && 'Account' !== $entries[$x]['sn'][0]){ $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0])); } } } ldap_unbind($ldap_connection); // Clean up after ourselves. } $message .= "Retrieved ". count($ad_users) ." Active Directory usersn"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- java poi 操作excel,xssf 读excel 2007,将某些
- php – Bootstrap 3 Datetimepicker在第二次单击
- Heroku PHP“Getting Started”不在OSX上本地运行
- php – 如何使这个MySQL Count查询更有效?
- Php Javascript将document.write的内容写入变量
- php – WordPress wp_schedule_event在30到60分钟
- BZOJ 2818: Gcd区间内最大公约数 为素数的对数(欧
- php – 在Symfony2上的测试环境中禁用安全防火墙
- php – WordPress:在管理员选项页面上传图片
- 在PHP中strrpos找到最后一个字符位置?
热点阅读