I'm porting an app to PHP8 and having an issue with ldap_sort no longer being available. I can retrieve the data from my ldap server and am able to sort on sn, then givenname but I get a warning on each term. I assume this is to do with incomplete data in some ldap records, but not sure. My code is below and the warning I get is:
Warning: Trying to access array offset on value of type int in C:\inetpub\wwwroot\inc\queries_php\ldap\qryldap.php on line 50
Warning: Trying to access array offset on value of type int in C:\inetpub\wwwroot\inc\queries_php\ldap\qryldap.php on line 51
Where line 50 is the one beginning $lastname and 51 is $givenname. I'm struggling to figure out a way to loop through the results in a way that prevents the warning showing. Any ideas please?
Calum
function QueryLDAP() {
// -------------- CHANGE VARIABLES TO SUIT YOUR ENVIRONMENT --------------
//LDAP server address
$server = "ldap://192.168.1.1";
//domain user to connect to LDAP
$user = "[email protected]";
//user password
$psw = "p@55w0rd!";
//FQDN path where search will be performed. OU - organizational unit / DC - domain component
$dn = "OU=Users,DC=Company,DC=local";
//Search query. CN - common name (CN=* will return all objects)
$search = "(&(objectCategory=person)(objectClass=user)(company=Company))";
$attributes = array("objectSid", "sAMAccountName", "givenname", "sn", "cn", "mail", "department", "telephonenumber", "mobile", "company", "title", "facsimileTelephoneNumber", "description");
// connecting to LDAP server
$ds=ldap_connect($server);
$r=ldap_bind($ds, $user , $psw);
// performing search
$sr=ldap_search($ds, $dn, $search, $attributes);
// Sort by lastname
// ldap_sort($ds, $sr, 'sn'); ldap_sort deprecated in PHP8 - see below!!
$GLOBALS['ldap'] = ldap_get_entries($ds, $sr); // allows this variable to be used in functions
// Sort the array by lastname, firstname ascending:
// from example 3, https://www.php.net/manual/en/function.array-multisort.php
foreach ($GLOBALS['ldap'] as $key => $row) {
$lastname[$key] = $row['sn'];
$firstname[$key] = $row['givenname'];
}
array_multisort($lastname, SORT_ASC, $firstname, SORT_ASC, $GLOBALS['ldap']);
// close connection
ldap_close($ds);
}
My Answer:
In PHP 8, the ldap_sort function has been deprecated and removed. To replace ldap_sort, you can manually sort the LDAP search results using PHP array functions like array_multisort.
In your code, you are already using array_multisort to sort the LDAP search results by lastname and firstname. However, you are getting warnings because some LDAP records may not have the 'sn' or 'givenname' attributes.
To prevent the warnings, you can check if the attributes exist before accessing them in the loop. Here's an updated version of your code:
php
function QueryLDAP() {
// Your existing code...
// Sort the array by lastname, firstname ascending:
$lastname = [];
$firstname = [];
foreach ($GLOBALS['ldap'] as $key => $row) {
$lastname[$key] = isset($row['sn']) ? $row['sn'] : '';
$firstname[$key] = isset($row['givenname']) ? $row['givenname'] : '';
}
array_multisort($lastname, SORT_ASC, $firstname, SORT_ASC, $GLOBALS['ldap']);
// close connection
ldap_close($ds);
}
By using the isset function to check if the 'sn' and 'givenname' attributes exist before accessing them, you can prevent the warnings about trying to access array offsets on values of type int. This way, the sorting will still work even if some LDAP records do not have these attributes.
Rate this post
3 of 5 based on 5891 votesComments