PHP Ldap connection example

ldap connection can be confusing. Its good to have a sample to follow:

$ldap_host = 'localhost';
$ldap_port = '389';
$ldap_username = 'uid=administrator,ou=admingroup,dc=company,dc=com,dc=au';
$ldap_passwd = 'mypass';
$ldap_testuser = 'testuser';
$ldap_dn = 'ou=customers,dc=domain,dc=com,dc=au';

// start connecting
$ldap_connect = ldap_connect($ldap_host, $ldap_port) or die("Could not connect to ".self::$ldap_host);
ldap_set_option($ldap_connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind($ldap_connect, $ldap_username, $ldap_passwd);

// do searches
$result = ldap_search($ldap_connect, $ldap_dn, "(uid=$username)") or die ("Error in query");
 $ldapdata = ldap_get_entries($ldap_connect, $result);

// delete user
ldap_delete($ldap_connect, "uid=$username,".$ldap_dn) or die (ldap_errno($ldap_connect));

// close connection

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.