File: fetch_entry.php

package info (click to toggle)
php-net-ldap 1%3A1.1.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 484 kB
  • ctags: 1,095
  • sloc: php: 2,669; xml: 431; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
/**
* This is a short example on how to fetch a specific entry in the
* directory using Net_LDAP.
*/

// We use the connecting.php example to get a link to our server.
// This file will also include all required basic Net_LDAP classes.
include_once 'connecting.php';

// Okay, we should have a valid link now.
// Lets fetch an entry! We want to know the admins first and last name.
// If we need additional attributes later, we must refetch the entry.
// It is a good practice to only select the attributes really needed.
// Since we want to be a little flexible, we make the base
// dynamic, so it is enough to change the base-dn in your
// $ldap_config array.
$entry = $ldap->getEntry('cn=admin,'.$ldap_config['base'], array('gn', 'sn'));

// Error checking is important!
if (Net_LDAP::isError($entry)) {
    die('Could not fetch entry: '.$entry->getMessage());
}

// Now fetch the data from the entry
$surename  = $entry->getValue('sn', 'single');
if (Net_LDAP::isError($surename)) {
    die('Unable to get surename: '.$surename->getMessage());
}
$givenname = $entry->getValue('gn', 'single');
if (Net_LDAP::isError($givenname)) {
    die('Unable to get surename: '.$givenname->getMessage());
}

// Finally output the data of the entry:
// This will give something like "Name of cn=admin,o=example,dc=org: Foo Bar"
echo 'Name of '.$entry->DN().': '.$givenname.' '.$surename;
?>