&reftitle.examples;
Basic usage Retrieve information for all entries where the surname starts with "S" from a directory server, displaying an extract with name and email address. LDAP search example LDAP query test"; echo "Connecting ..."; $ds=ldap_connect("localhost"); // must be a valid LDAP server! echo "connect result is " . $ds . "
"; if ($ds) { echo "Binding ..."; $r=ldap_bind($ds); // this is an "anonymous" bind, typically // read-only access echo "Bind result is " . $r . "
"; echo "Searching for (sn=S*) ..."; // Search surname entry $sr=ldap_search($ds, "o=My Company, c=US", "sn=S*"); echo "Search result is " . $sr . "
"; echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "
"; echo "Getting entries ...

"; $info = ldap_get_entries($ds, $sr); echo "Data for " . $info["count"] . " items returned:

"; for ($i=0; $i<$info["count"]; $i++) { echo "dn is: " . $info[$i]["dn"] . "
"; echo "first cn entry is: " . $info[$i]["cn"][0] . "
"; echo "first email entry is: " . $info[$i]["mail"][0] . "


"; } echo "Closing connection"; ldap_close($ds); } else { echo "

Unable to connect to LDAP server

"; } ?> ]]>
LDAP Controls Here are some examples of using LDAP Controls with PHP >= 7.3.0. Bind with ppolicy information LDAP_CONTROL_PASSWORDPOLICYREQUEST]]); if (ldap_parse_result($ds, $r, $errcode, $matcheddn, $errmsg, $referrals, $ctrls)) { if ($errcode != 0) { die("Error: $errmsg ($errcode)"); } if (isset($ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE])) { $value = $ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE]['value']; echo "Expires in: ".$value['expire']." seconds\n"; echo "Number of auth left: ".$value['grace']."\n"; if (isset($value['error'])) { echo "Ppolicy error code: ".$value['error']; } } } } else { die("Unable to connect to LDAP server"); } ?> ]]> Modify description only if it's not empty 'New description'], [ [ 'oid' => LDAP_CONTROL_ASSERT, 'iscritical' => TRUE, 'value' => ['filter' => '(!(description=*))'] ] ] ); // Then use ldap_parse_result ?> ]]> Read some values before deletion LDAP_CONTROL_PRE_READ, 'iscritical' => TRUE, 'value' => ['attrs' => ['o', 'description']] ] ] ); // Then use ldap_parse_result ?> ]]> Delete a reference LDAP_CONTROL_MANAGEDSAIT, 'iscritical' => TRUE]] ); // Then use ldap_parse_result ?> ]]> Use pagination for a search LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => 2, 'cookie' => $cookie]]] ); ldap_parse_result($link, $result, $errcode , $matcheddn , $errmsg , $referrals, $controls); // To keep the example short errors are not tested $entries = ldap_get_entries($link, $result); foreach ($entries as $entry) { echo "cn: ".$entry['cn'][0]."\n"; } if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) { // You need to pass the cookie from the last call to the next one $cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie']; } else { $cookie = ''; } // Empty cookie means last page } while (!empty($cookie)); ?> ]]>