PHP
  Home arrow PHP arrow Page 5 - Using PHP With LDAP (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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 2)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2003-04-07


    Table of Contents:
  • Using PHP With LDAP (part 2)
  • Of Needles And Haystacks
  • Making Lists
  • Adding It All Up
  • Changing Things Around
  • Wiping Out The Past
  • To Err Is Human...
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 2) - Changing Things Around
    ( Page 5 of 8 )

    Next up, modifying entries. You might remember, from previous pages, that the index page of this application included links to scripts named "edit.php" and "delete.php" next to each entry, and passed each of those scripts certain data using the URL GET method. Here's what that code looked like:








    <td> <a href=\"edit.php?mail=" . urlencode($info[$i]["mail"][0]) . "\">Edit</a> </td>
    As you can see, the user's email address is passed from the main index page to the "edit.php" script on the URL when the user clicks the corresponding hyperlink. This email address can then be used, in combination with the ldap_list() function, to retrieve the complete user record and display it in a form for editing - which is exactly what the next script does:

    <html> <head> </head> <body> <table width="450" cellpadding="5" cellspacing="5" border="1"> <?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"); // set base DN and return attribute list $base_dn = "dc=my-domain,dc=com"; $params = array("mail", "cn", "sn"); // perform search using email address passed on URL $result = ldap_list($conn, $base_dn, "mail=" . urldecode($_GET['mail']), $params); // extract data into array $info = ldap_get_entries($conn, $result); // print and display as editable form ?> <form method="POST" action="modify.php"> <table border="0" cellpadding="0" cellspacing="10" width="500" > <tr> <td width="50%" align="right">First Name</td> <td width="50%"><input type="text" name="cn" size="20" value="<?php echo $info[0]["cn"][0]; ?>"></td> </tr> <tr> <td width="50%" align="right">Last Name</td> <td width="50%"><input type="text" name="sn" size="20" value="<?php echo $info[0]["sn"][0]; ?>"></td> </tr> <tr> <td width="50%" align="right">E-mail</td> <td width="50%"><input type="text" name="mail" size="20" value="<?php echo $info[0]["mail"][0]; ?>"></td> </tr> <tr> <td width="100%" colspan="2" align="center"> <input type="submit" value="Submit" name="Submit"> &nbsp;&nbsp;<input type="reset" value="Reset" name="Reset"> </td> </tr> </table> </form> <?php // all done? clean up ldap_close($conn); ?> </table> </body> </html>
    Here's what it looks like:



    I can now edit the data in the HTML form and submit it to a script that updates the entry in the directory. Here's the PHP code that take care of that:

    <html> <head> </head> <body> <?php // specify the LDAP server to connect to $conn = ldap_connect("localhost") or die("Could not connect to server. Error is " . ldap_error($conn)); // user with privileges to add an entry to LDAP hierarchy $root_dn = "cn=root, dc=my-domain, dc=com"; $root_pw = "secret"; // bind to the LDAP server specified above $r = ldap_bind($conn, $root_dn, $root_pw) or die("Could not bind to server. Error is " . ldap_error($conn)); // prepare data $info["cn"] = $_POST['cn']; $info["sn"] = $_POST['sn']; $info["mail"] = $_POST['mail']; $info["objectClass"] = "inetOrgPerson"; // prepare DN $dn = "mail=" . $_POST['mail'] . ", dc=my-domain, dc=com"; // modify data in the directory $result = ldap_modify($conn, $dn, $info); // if successful, display success message if($result) { echo "Entry with DN " . $dn . " modified in LDAP directory."; } // else display error else { echo "An error occurred. Error number " . ldap_errno($conn) . ": " . ldap_err2str(ldap_errno($conn)); } // all done? clean up ldap_close($conn); ?> </body> </html>
    This code listing is similar to the code for adding a new user...with one important change: it uses the ldap_modify() function instead of the ldap_add() command to update an existing entry, rather than add a new one.

    Here's the output of the script above, indicating that the update was successful:



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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