1
votes

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>
2
lol, you are on a roll here - its your third question on the same issue. I see you are progressing slowly :) btw what does print_r($info) give? Cause I think it would probably be: echo count($info)raidenace
Nice password too...Sebastien

2 Answers

1
votes

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);
0
votes

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

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