PHP导出ldap,如何使用PHP从LDAP目录获取用户列表?

本文档展示了如何使用PHP连接到Active Directory LDAP服务器并检索用户列表。在尝试从LDAP获取用户列表时遇到'搜索:操作错误'的警告。问题在于搜索过滤器。修正后的代码提供了一个有效的过滤器,允许获取所有用户的基本信息,如名字、邮箱、用户名和姓氏,并将结果存储在一个关联数组中。此外,还包含了设置LDAP选项、身份验证和资源清理的步骤。
摘要由CSDN通过智能技术生成

$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, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');

ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 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

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 users\n";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值