PHP
  Home arrow PHP arrow Page 6 - Using PHP With LDAP (part 1)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using PHP With LDAP (part 1)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 18
    2003-04-04

    Table of Contents:
  • Using PHP With LDAP (part 1)
  • Looking For Answers
  • The Bare Necessities
  • Code Poet
  • Anatomy 101
  • What's In A Name?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using PHP With LDAP (part 1) - What's In A Name?


    (Page 6 of 6 )

    The example on the previous page was fairly static - all it did was return a list of all the entries in the directory that had a "cn" attribute. This next example makes things a little more interactive - it includes a form where the user can enter a name and search the LDAP server for that name.

    Here's the form,

    <html> <head> <title>Search</title> </head> <body> <form action="search.php" method="POST"> <input type="text" name="name" length="30"> <input type="submit" name="submit" value = "Search"> </form> </body> </html>
    and here's what it looks like:



    And here's the code for the search script:

    <html> <head> </head> <body> <?php // specify the LDAP server to connect to $conn = ldap_connect("localhost") or die("Could not connect to server"); // bind to the LDAP server specified above $r = ldap_bind($conn) or die("Could not bind to server"); // create the search string $query = "(cn=" . $_POST['name'] . ")"; // start searching // specify both the start location and the search criteria // in this case, start at the top and return all entries $result = ldap_search($conn,"dc=my-domain,dc=com", $query) or die ("Error in search query"); // get entry data as array $info = ldap_get_entries($conn, $result); // iterate over array and print data for each entry echo "<ul>"; for ($i=0; $i<$info["count"]; $i++) { echo "<li>" . $info[$i]["cn"][0] ." - ".$info[$i]["mail"][0] . "</li>"; } echo "</ul>"; // print number of entries found echo "Number of entries found: " . ldap_count_entries($conn, $result) . "<p>"; // all done? clean up ldap_close($conn); ?> </body> </html>
    The only difference between this script and the previous one is that this time, the search criteria is dynamically generated using the data POSTed from the form.

    $query = "(cn=" . $_POST['name'] . ")";
    This parameter is then passed to the ldap_search() function and processed in the standard manner.

    Here's what you should see, assuming that you searched for the string "joe":



    Simple when you know how, isn't it?

    And that's about it for this first part. In the second part of this article, I will be showing you how to carry out more complex searches, and also add and delete information from the LDAP directory. Stay tuned for that one.and, until next time, stay healthy!

    Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · great article. it would be cool if you added a couple more pages explaining how to...
       · I agree great article but would be nice to have it expanded on. :)
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT