| 
<html><head>
 <title> class.ldap.php4: Example1 </title>
 </head>
 <body>
 <?php
 
 include('class.ldap.php4');
 
 // The hostname of our LDAP server
 $ServerHost = 'localhost';
 
 // The base DN we'll be querying (saves lots of typing)
 $BaseDN = 'ou=staff,dc=example,dc=org';
 
 // The user we're going to try to log in as
 $Username = 'fred';
 
 // The user's password
 $Password = 'barny';
 
 // Create an ldap object
 $ld = new ldap($ServerHost);
 
 // Connect to the server
 if (!$ld->connect()) {
 die("Error connecting: ".$ld->ldapError."\n");
 }
 
 // We'll try to log in as a user
 if ($ld->bind("uid=$Username,$BaseDN",$Password)) {
 // Example of the added 'children' function
 // Get a list of child nodes under our username
 if ($children = $ld->children("uid=$Username,$BaseDN")) {
 print "<b>Children under our $Username's node</b><pre>\n";
 var_dump($children);
 print "</pre>\n";
 } else {
 print "Error getting children: ".$ld->ldapError."<br>\n";
 }
 
 // Find an entry under the address book
 // All search functions return ldapresult objects
 if ($sr = $ld->searchSubtree("ou=AddressBook,dc=example,dc=org","givenName=wilma",array('cn','givenName','sn','homePhone'))) {
 // Get the first entry that the search returns
 // ldapresult->firstEntry returns ldapresultentry objects
 if ($entry = $sr->firstEntry()) {
 printEntry($entry);
 // More then one result?
 // ldapresultentry->nextEntry() simply updates the existing entry
 while ($entry->nextEntry()) {
 printEntry($entry);
 }
 } else {
 die("Error fetching entry: ".$sr->ldapError."\n");
 }
 // Good idea if you're doing lots of large queries, but not required
 $sr->free();
 } else {
 die("Error performing search: ".$ld->ldapError."\n");
 }
 } else {
 die("Error binding: ".$ld->ldapError."\n");
 }
 
 
 function printEntry($entry) {
 print "<b>DN:</b>".$entry->getDN();
 // Fetch all the attributes
 if ($attrs = $entry->getAttributes()) {
 print "<b>cn:</b> ".$attrs['cn']."<br>\n";
 print "<b>givenName:</b> ".$attrs['givenName']."<br>\n";
 print "<b>sn:</b> ".$attrs['sn']."<br>\n";
 print "<b>homePhone</b> ".$attrs['homePhone']."<br>\n";
 } else {
 print "Error while fetching attributes: ".$entry->ldapError."<br>\n";
 }
 }
 ?>
 </body>
 </html>
 |