eldapo does php

Been experimenting with PHP for a few days — finally. My friend Gary Utz was after me a couple of years ago to do this, and I even bought a couple of O’Reilly books on it.

Well, here I am. Of course, rather than do something simple like write a MySQL database app, I’m working on a simple LDAP white pages lookup thing.

Here’s my code “sandbox”, a little script I’ll be adding and removing from until I get something decent. Right now it just searches for an entry and prints out some info.

<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection
echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$dirHost = "ldap.example.com";
$usrbase = "dc=example,dc=com";
$query = "title=IT Systems Analyst";

$ds=ldap_connect($dirHost);  // must be a valid LDAP server!
echo "connect result is " . $ds . "<br />";

if ($ds) {
    echo "Binding ...";
    $r=ldap_bind($ds);     // this is an "anonymous" bind, typically
                           // read-only access
    echo "Bind result is " . $r . "<br />";

    echo "Searching for (sn=S*) ...";
    // Search surname entry
    $sr=ldap_search($ds, $usrbase, $query);
    echo "Search result is " . $sr . "<br />";

    echo "Number of entires returned is " . ldap_count_entries($ds, $sr) . "<br />";

    echo "Getting entries ...<p>";
    $info = ldap_get_entries($ds, $sr);
    echo "Data for " . $info["count"] . " items returned:<p>";

    for ($i=0; $i<$info["count"]; $i++) {
        echo "dn is: " . $info[$i]["dn"] . "<br />";
        echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
        echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
    }

    echo "Closing connection";
    ldap_close($ds);

} else {
    echo "<h4>Unable to connect to LDAP server</h4>";
}
?>

My first impression is that Perl’s Net::LDAP module, which has been the mainstay of my daily activities for almost 7 years, is easier to work with. Of course, I’ve been writing scripts in Perl so long that its syntax has become second nature. PHP generally reminds me of Javascript or VBScript more than anything else. One thing I’m going to be interested in finding out is how you’d do a Simple Paged Results search using php-ldap (if it can be done at all), which is critical to working with Active Directory over the LDAP protocol.