How To Do An LDAP Add Using PHP

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:

// connect to LDAP server
$ldapconn = ldap_connect("ldap://") 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" => "",
    "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

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.

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.