To add a new LDAP directory entry using PHP, you can use the ldap_add()
function. Here’s an example of how to use it:
<?php
// connect to LDAP server
$ldapconn = ldap_connect("ldap://ldap.example.com") or die("Could not connect to LDAP server.");
// bind to LDAP server with a privileged account
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "adminpassword") or die("Could not bind to LDAP server.");
// specify the DN of the new entry to add
$dn = "cn=John Doe,ou=Users,dc=example,dc=com";
// specify the attributes of the new entry
$attributes = array(
"objectClass" => array("top", "person", "organizationalPerson", "inetOrgPerson"),
"cn" => "John Doe",
"givenName" => "John",
"sn" => "Doe",
"mail" => "johndoe@example.com",
"userPassword" => "{SHA}8qO3i9Ox9n9ySpndB0R1fVmKwvY="
);
// add the new entry
if (ldap_add($ldapconn, $dn, $attributes)) {
echo "New LDAP entry added successfully.";
} else {
echo "Error adding new LDAP entry: " . ldap_error($ldapconn);
}
// close LDAP connection
ldap_close($ldapconn);
?>
In this example, we’re connecting to an LDAP server, binding with a privileged account, specifying the DN of the new entry to add, and then specifying the attributes of the new entry using an array of attribute-value pairs. Finally, we add the new entry with ldap_add()
and print out a success or error message. Don’t forget to close the LDAP connection when you’re done.