How can a PHP script be structured to efficiently check if a user is in a predefined group in OpenLDAP?
To efficiently check if a user is in a predefined group in OpenLDAP using PHP, you can utilize the ldap_search function to search for the user's entry and then check if they belong to the specified group by comparing their group membership attribute.
<?php
$ldapconn = ldap_connect("ldap.example.com");
if ($ldapconn) {
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");
$result = ldap_search($ldapconn, "ou=groups,dc=example,dc=com", "(member=uid=user,ou=users,dc=example,dc=com)");
$entries = ldap_get_entries($ldapconn, $result);
if ($entries['count'] > 0) {
echo "User is in the predefined group.";
} else {
echo "User is not in the predefined group.";
}
ldap_close($ldapconn);
} else {
echo "Unable to connect to LDAP server.";
}
?>
Related questions
-
PHP number formatting sprintf
-
require include variables
-
variable assignments passing PHP scripts