Home arrow PHP arrow Page 7 - Using PHP With LDAP (part 2)

To Err Is Human... - PHP

In this second, and concluding, article, dig deeper into PHP'sLDAP API by writing complex search queries and building a Web-basedadministration module to retrieve and modify entries from the LDAP directorytree.

TABLE OF CONTENTS:
  1. Using PHP With LDAP (part 2)
  2. Of Needles And Haystacks
  3. Making Lists
  4. Adding It All Up
  5. Changing Things Around
  6. Wiping Out The Past
  7. To Err Is Human...
  8. Endgame
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 9
April 07, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
You might remember, from the scripts in this article, my copious use of the ldap_error() and ldap_errno() functions. As you must have guessed by now, these are built-in API functions to record and display error messages.

The ldap_errno() function returns a pre-defined error number for each LDAP error. While this number is, by itself, not very useful, it acquires significance when coupled with yet another PHP function, ldap_err2str(), which returns a user-friendly error message for display to the user.

In order to see how this function may be used, consider the next example, which uses the ldap_error() and ldap_err2str() functions to trap and generate the error message resulting from an attempt to bind to a non-existent LDAP server:


<html> <head> </head> <body> <?php // specify the LDAP server to connect to $conn = ldap_connect("www.somewhere.com") or die("Could not connect to server"); // bind to the LDAP server specified above $r = ldap_bind($conn); // if not successful, display error message if(!$r) { echo "An error occurred. Error number " . ldap_errno($conn) . ": " . ldap_err2str(ldap_errno($conn)); } // further processing as required // all done? clean up ldap_close($conn); ?> </body> </html>
Here's what the output looks like:



There's also a shortcut - the ldap_error() function, which returns the last error message generated. The following code snippet, which is equivalent to the one above, demonstrates:

<html> <head> </head> <body> <?php // specify the LDAP server to connect to $conn = ldap_connect("www.somewhere.com") or die("Could not connect to server");; // bind to the LDAP server specified above $r = ldap_bind($conn); // if not successful, display display error message if(!$r) { echo "An error occurred - " . ldap_error($conn); } // further processing as required // all done? clean up ldap_close($conn); ?> </body> </html>


 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: