1
votes

I'm having troubles with ldap_search in PHP. The query below gives LDAP ERROR "-7" - Bad search filter.
Code snippet:

...
$base_dn = "ou=Example GSM,dc=example,dc=com";
$search_filter = "(userPrincipalName=example\name.surname)";
$bind_attr = "userPrincipalName";
$result = @ldap_search($ldapconn, $base_dn, $search_filter, array("dn", $bind_attr));

Do you an have idea what I'm doing wrong?

I'm using Microsoft AD and PHP 7.2.18 .

1

1 Answers

0
votes

I suppose that the \ needs to be escaped for the filter to work.

You should be able to get what you want by using this way to define your search_filter:

$search_filter = "(userPrincipalName=" . ldap_escape('example\name.surname', '', LDAP_ESCAPE_FILTER) . ")";

For more info have a look at the documentation at https://www.php.net/ldap_escape

Also have a look at the user-contributed notes at https://www.php.net/manual/en/function.ldap-escape.php#118127 which seems to describe your exact problem