How can the organizational unit (OU) of a computer be dynamically determined and updated in LDAP using PHP?

To dynamically determine and update the organizational unit (OU) of a computer in LDAP using PHP, you can retrieve the necessary information from the computer itself or from a database, and then use LDAP functions in PHP to update the OU attribute of the computer entry in the LDAP directory.

// Connect to LDAP server
$ldapconn = ldap_connect("ldap://yourldapserver.com");

// Bind to LDAP server
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");

// Get computer's DN based on its common name
$computer_cn = "computer1";
$dn = "cn=" . $computer_cn . ",ou=computers,dc=example,dc=com";

// Update the OU attribute for the computer entry
$ou = "ou=newou,dc=example,dc=com";
$entry = array('ou' => $ou);
ldap_modify($ldapconn, $dn, $entry);

// Close LDAP connection
ldap_close($ldapconn);