Gayane
Gayane

Reputation: 637

LDAP search get user givenname by userid

I have successfully run ldap_connect and ldap_bind commands in my php script. Now I need to get guvenName by user id. How can i do this.

$username = $_POST['username'];
$password = $_POST['password'];

define('LDAP_SERVER', 'localhost');
define('LDAP_PORT', 389);
define('LDAP_TOP', 'ou=people,dc=domain,dc=com');

$ds = ldap_connect(LDAP_SERVER, LDAP_PORT);
if (!$ds) {
    echo "FALSE";
}
if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
    @ldap_close($ds);
    echo "FALSE";
}
$dn = 'uid=' . $username . "," . LDAP_TOP;

if (!ldap_bind($ds, $dn, $password)) {
    echo "FALSE";
}

Upvotes: 2

Views: 8404

Answers (1)

rkosegi
rkosegi

Reputation: 14658

In general it's quite simple

public function getusercn($accountname)
{
     $filter_person = "(&(sAMAccountName={$accountname}))";
     $sr_person = ldap_search($this->ds_ad,$this->base_user_dn,$filter_person);
     $sr = ldap_get_entries($this->ds_ad, $sr_person);
     $attr = $sr[0]["givenName"][0];
     return $attr;
}

$this->ds_ad - it's $ds in your code

$this->base_user_dn - is base OU from which you want to search (like LDAP_TOP in your case)

sAMAccountName - is "user id" attribute in your case uid

givenName - is attribute what you are looking for

All of attributes are already in $sr variable, so you can use var_dump to inspect all content.

Upvotes: 2

Related Questions