user1214840
user1214840

Reputation: 189

Searching for email address ldap active directory

I am trying to search an active directory using ldap. I want to be able to return the users email address. How can this be done? So far I have the following, but nothing seems to happen.

I just want to return mail based on the attributes given in $filter. The ldap bind seems to work fine.

Thanks :)

<!DOCTYPE HTML>
<html>
<head>
<title>Cisco Guest Register</title>
</head>

<body>

<?php


$ldaprdn  = "CN=antwest,OU=Employees,OU=Cisco Users,DC=cisco,DC=com";    
$ldappass = 'Chandler1';

// connect to ldap server
$ldapconn = ldap_connect("ldap://ds.cisco.com:389")
or die("Could not connect to LDAP server.");

if ($ldapconn) {

// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

// verify binding
if (!$ldapbind) {
    echo "Connection to LDAP Failed";
}

echo "Connected to LDAP";

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS,0);

$filter="(|(cn=antwest*)(ou=cisco*))";

$justthese = array("mail");

$sr=ldap_search($ldapconn, $ldaprdn, $filter, $justthese);

$info = ldap_get_entries($ldapconn, $sr);

echo $info["count"]." entries returned\n";

}
?>

</body>

</html>

Upvotes: 1

Views: 4122

Answers (2)

Alex Joe
Alex Joe

Reputation: 389

It's important to set ldap_set_option before call ldap_bind:

$ldapconn = ldap_connect("ldap://ds.cisco.com:389");

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS,0);

$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

Upvotes: 1

Hackerman
Hackerman

Reputation: 12295

To print just the email, and if your search is succeded, then use this line:

echo $info[0]["mail"][0];

Upvotes: 0

Related Questions