Below is basic PHP script which demonstrates MySQL and Active Directory communication :
";
mysql_close($dblink);
/* Going a step forward now we will connect to ldap server */
$ldapserver = "xxxx";
$user = "cn=Ashish Srivastava,cn=Users,dc=mydomamin,dc=com";
$passwd = "ashish";
$base = "cn=Users,dc=mydomamin,dc=com";
// Connect to ldap server
$dsCon = ldap_connect($ldapserver);
// Make sure we connected
if (!($dsCon))
{
print("Sorry, cannot contact LDAP server");
exit(2);
}
// Set Active Directory Specific Options
ldap_set_option($dsCon,LDAP_OPT_REFERRALS, 0);
// Bind LDAP
$bindresult = ldap_bind($dsCon,$user,$passwd);
if(!$bindresult){
$errno = ldap_errno($dsCon);
$error = ldap_error($dsCon);
print "LDAP Error ($errno): $error";
exit(3);
}
// Now Perform Search
$attributes = array("cn"); // attributes which you want
$search_filter = "(&(objectclass=user)(objectcategory=user)(sAMAccountName=ashish.srivastava))";
$search_result=ldap_search($dsCon, $base, $search_filter, $attributes);
if(!$search_result){
$errno = ldap_errno($dsCon);
$error = ldap_error($dsCon);
print "LDAP Error ($errno): $error";
exit(4);
}
// Getting Entries
$entry = ldap_get_entries($dsCon,$search_result);
if(!$entry){
$errno = ldap_errno($dsCon);
$error = ldap_error($dsCon);
print "LDAP Error ($errno): $error";
exit(4);
}
$cn = $entry[0]["cn"][0];
//var_dump($entry);
print "Common Name: $cn
";
ldap_close($dsCon);
?>
No comments:
Post a Comment