Logo

LDAP connect

Posted under » PHP on 25 October 2009

This script works great.

<¿php
$ldap_host = "ldap.php.net.sg";
$ldap_port = "389";

$base_dn = "dc=php,dc=net,dc=sg";
$filter = "(cn=hanafi)";
$ldap_user  = "cn=hanafi,ou=people,dc=php,dc=net,dc=sg";
$ldap_pass = "taik";
$connect = ldap_connect( $ldap_host, $ldap_port)
         or exit(">>Could not connect to LDAP server<<");
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($connect, $ldap_user, $ldap_pass)
      or exit(">>Could not bind to $ldap_host<<");
$read = ldap_search($connect, $base_dn, $filter)
      or exit(">>Unable to search ldap server<<");
$info = ldap_get_entries($connect, $read);
echo $info["count"]." entries returned

"; $ii=0; for ($i=0; $ii<$info[$i]["count"]; $ii++){ $data = $info[$i][$ii]; echo $data.": ".$info[$i][$data][0]."
"; } ldap_close($connect); ?>

There is another one where you can check anonymously (ie without password) but I won't show it here because most LDAP server do not have give you that right.