Beware of the case sensitivity in php and LDAP/AD connections!

Ok, things do not behave always as they should!
One of those frustrations is case sensitivity when you try use ldap queries from php code.
Consider the following example:
$ldapuser = "user";
$ldappass = "pass";
$basedn = "OU=People,DC=staff,DC=company,DC=com";
$domain = "";
$ldaphost = "";
$LDAPFieldsToFind = array("cn", "mail", "sAMAccountName");

// connect to ldap server
$ldapconn = ldap_connect($ldaphost);

// Setting Active Directory
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

// binding to ldap server
$ldapbind = @ldap_bind($ldapconn, "{$ldapuser}@{$domain}", $ldappass);
echo "\nSearching in base_dn $basedn - filter: $filter\n";
$userdetails = ldap_search($ldapconn, $basedn, $filter, $LDAPFieldsToFind);
$info = ldap_get_entries($ldapconn, $userdetails);

  for ($x=0; $x<$info["count"]; $x++) {
      print "\nCN is: $nam \n";
      print "Mail is: $email\n";
      print "Uid: $samaccountname\n";

Although the above piece of code is perfectly right, you won't get any results!

You will also get a message similar to:
PHP Notice:  Undefined index: sAMAccountName in xxx.php on line xxx

Why?? Because for some strange reason, the attributes should be in lower case.
Therefore if you change sAMAccountName to samaccountname everything will work as expected...

The lines that should modified are:
$LDAPFieldsToFind = array("cn", "mail", "samaccountname"); 
Try it for yourself, and post your comments in case you experience something different...


  1. Yes! I met the same issue...
    It's weird.

  2. there is no result because the sensitive case for the letters
    how can i let ignore the case letters

  3. BlueHost is the best hosting company for any hosting services you might require.

  4. replica Alexander McQueen shoes, combining elegant style and cutting-edge technology, a variety of styles of Cheap Alexander McQueen womens shoes, the pointer walks between your exclusive taste style.