A Sample LDAP Application in PHP Page 21
February 22, 2002
We collect the attributes of the new entry to be added in an
associative array:
$entryToAdd["cn"] = $cn;
$entryToAdd["sn"] = $sn;
$entryToAdd["mail"] = $mail;
$entryToAdd["employeenumber"] = $employeenumber;
$entryToAdd["ou"] = $ou;
$entryToAdd["telephonenumber"] = $telephonenumber;
$entryToAdd["objectclass"] = "person";
$entryToAdd["objectclass"] = "organizationalPerson";
$entryToAdd["objectclass"] = "inetOrgPerson";
Here we construct the DN corresponding to the new entry:
$dnString = "mail=" . $mail . "," . "ou=". $ou . "," . $baseDN;
This is the root DN we shall bind to, before performing the add
operation:
$adminRDN = "cn=Admin," . $baseDN;
We connect to the server and bind as an administrator:
$linkIdentifier = connectBindServer($adminRDN, $adminpassword);
if ($linkIdentifier) {
The actual addition is done here:
if (ldap_add($linkIdentifier, $dnString,
$entryToAdd) == true) {
generateHTMLHeader("The entry was added succesfully");
returnToMain();
} else {
displayErrMsg("Addition to directory failed ! !");
closeConnection($linkIdentifier);
returnToMain();
exit;
}
} else {
displayErrMsg("Connection to LDAP server failed!");
exit;
}
}
}
?>
[The red lines above are one line. They have been split for
formatting purposes.]
[In the code above, there should be no space between the
double "!" marks. They have been separated for formatting
purposes.]
A typical screen prompting the user to enter the attributes would
like below:
We need to be aware of certain caveats with this application that
arise from the fact that this is merely illustrative of the PHP
LDAP API and not a fully-fledged production application. As
mentioned before the use of HTTP sessions is highly recommended
to indicate authentication status. Further users created using
the add mechanism do not have a password field due to which
modification of such entries is not possible through the current
mechanism.
A Sample LDAP Application in PHP Page 20
Professional PHP4 Programming
A Sample LDAP Application in PHP Page 22
|