简体   繁体   English

如何通过对象的真实DN搜索php中的ldap?

[英]How do you search ldap in php by the true DN of the object?

The problem I am having is that the string $named2 will work string $named1 will not return any results... 我遇到的问题是字符串$named2将起作用字符串$named1将不返回任何结果...

Here is my example: 这是我的示例:

$root_dn = "DC=na,DC=company,DC=net";

$named1 = "CN=Murray\, James,OU=Users,OU=USA,DC=na,DC=company,DC=net";

$named2 = "CN=empNum12345,OU=Users,OU=USA,DC=na,DC=company,DC=net";


$named_arr = explode("OU=",$named1,15);
$named_arr[0] = rtrim($named_arr[0], ',');

$ldap_query = $named_arr[0]; //CN=Murray\, James

if($connect_id) {

    ldap_set_option($connect_id, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($connect_id, LDAP_OPT_REFERRALS, 0);

    $bind = ldap_bind($connect_id, $LDAP_LGIN[$SERVER_ID], $LDAP_PASS[$SERVER_ID]);

    $sr=ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query);
    $info = ldap_get_entries($connect_id, $sr);
    $count = $info["count"];
    $rs = ldap_flatresults($connect_id, $sr);

    echo $rs;
}

Any ideas? 有任何想法吗?

The thing that seems to break your search is the escaped comma in the CN of James Murray. 似乎打乱您搜索范围的是James Murray的CN中的转义逗号。 It's passed over to the ldap_seach and that can't find anything with that search-string. 它已传递到ldap_seach并且该搜索字符串找不到任何内容。

So one solution would be to strip the backslash before passing the string to ldap_search like this: 因此,一种解决方案是在将字符串传递给ldap_search之前,先ldap_search反斜杠,如下所示:

$ldap_query = str_replace('\\', '', $named_arr[0]);

The alternative (and in my eyes easier) solution would be to use the DN you already have and fetch the data for exactly that DN by replacing your ldap_search with this: 另一种解决方案(在我看来更容易解决)是使用您已经拥有的DN并通过将ldap_search替换为以下内容来获取该DN的数据:

$sr = ldap_read($connect_id, $named1, "objectclass=*");

That should give you all attributes of James Murray. 那应该给你詹姆斯·默里的所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM