C O N O S C E N Z A
web data management
Il nostro è un team che si occupa ormai da piu' di un decennio della risoluzione delle problematiche informatiche, in particolare ci piace poter offrire la nostra competenza in ambito telematico, dalla progettazione su misura della vostra infrastruttura web, alla realizzazione del vostro software e dei vostri portali.
PHP LDAP example

Web

PHP LDAP example

06/12/2011
My notes


< ?php

#### MY PARAMS
#
#

$LDAPserver = 'localhost';
$LDAPschema = 'inetOrgPerson';
$LDAPname = 'givenname';
$LDAPsurname = 'sn';
$LDAPemail = 'mail';
#
#
##### MY PARAMS END



//THE CONNECTION
$myldap=ldap_connect($LDAPserver); // must be a valid LDAP server!

if ($myldap) {

####THE AUTENTICATION
#
#
// the ldap access data
$ldaprdn = 'cn=admin,dc=localhost'; // ldap dn
$ldappass = 'MyPassword'; // ldap password
// connect to the server
$ldapconn = ldap_connect("localhost")
or die("Failed to connect to LDAP server.");
if ($ldapconn) {
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
// verify binding
if (!$ldapbind) {
echo "LDAP bind failed!";
}
}
#
#
##### THE AUTENTICATION END



####THE SEARCH METHOD
#
#
// search filter. We'll search only in a object Class
$filter = "(objectClass=$LDAPschema)";
//and only for a cn user (myuser) on localhostServer
if (!($search = ldap_search($ldapconn, "cn=myuser,dc=localhost", $filter))) {
die("Can't search in ldap server");
}
// number of returned results
echo ldap_count_entries($ldapconn,$search);
//retry results
$Myresult = ldap_get_entries($ldapconn, $search);
//showing results
if($Myresult[0]["cn"][0]){
echo "User is: ". $Myresult[0]["cn"][0]."
";
echo "Name is: ". $Myresult[0][$LDAPname][0]."
";
echo "Surname is: ". $Myresult[0][$LDAPsurname][0]."
";
echo "Email is: ". $Myresult[0][$LDAPemail][0]."
";
}
#
#
##### THE SEARCH METHOD END



####THE MODIFY METHOD
#
#
// create the data array to modify
$entryMod[$LDAPname]=array('Myfirstname');
// update utente
ldap_modify ($ldapconn, "cn=myuser,dc=localhost", $entryMod);
#
#
##### THE MODIFY METHOD END


####THE ADD METHOD
#
#
// create the data array to insert
$infoAdd["cn"]= array("MYNAMEid");
$infoAdd[$LDAPsurname]=array("MYsurname");
$infoAdd[$LDAPemail]=array("my@mail.ex");
$infoAdd["userPassword"]=array("Mypassword");
$infoAdd["objectclass"]=array($LDAPschema);
// insertr data
$r=ldap_add($ldapconn, "cn=".$infoAdd["cn"][0].", dc=localhost", $infoAdd);
#
#
##### THE ADD METHOD END


echo "Closing connection";
ldap_close($ds);

} else {
echo "Can't connect to LDAP server";
}
?>