How To Do An LDAP Modify Using PHP

To modify an LDAP directory entry using PHP, you can use the ldap_modify() 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 entry to modify
$dn = "cn=John Doe,ou=Users,dc=example,dc=com";

// specify the modifications to make
$modifications = array(
    array(
        "attrib" => "givenName",
        "modtype" => LDAP_MODIFY_BATCH_REPLACE,
        "values" => array("Jonathan")
    ),
    array(
        "attrib" => "sn",
        "modtype" => LDAP_MODIFY_BATCH_REPLACE,
        "values" => array("Smith")
    )
);

// perform the modification
if (ldap_modify($ldapconn, $dn, $modifications)) {
    echo "LDAP entry modified successfully.";
} else {
    echo "Error modifying 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 entry to modify, and then specifying the modifications to make using an array of modification objects. Finally, we perform the modification with ldap_modify() 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.