Jump to content

PHP & LDAP - Please help I am going mad!


dmccabe

Recommended Posts

I have been messing with php and ldap for the last couple of months in the vain hope of creating an internal searchable address book.

 

However no matter what example scripts I use or what I try I am never able to get any results.

 

I am not a php developer or expert in anyway, but am pretty good at understanding and modifying files for my needs, yet I cannot get anything from ldap.

 

I would really appreciate anyone who can give me a working example of a script that will search a username and then help me get it working on my ldap server.

 

For example I have copied this script from php.net http://uk2.php.net/ldap#48343

 

<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection

echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect("lch-ad.lch.com");  // 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, "o=1car1users", "sn=S*");  
    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>";
}
?>

 

The results I get is:

 

LDAP query test

Connecting ...connect result is Resource id #1

Binding ...Bind result is 1

Searching for (sn=S*) ...Search result is

Number of entires returned is

Getting entries ...

 

Data for items returned:

 

Closing connection

 

Which seems to indicate it is connecting as I would have expected the Unable to connect to LDAP server otherwise, yet it returns no results ?

 

Please help!

Link to comment
Share on other sites

I found another script which made things easier to understand and have managed to get it outputting results!  HOORAH.

 

Have a new question but will start a new thread as it's not really ldap related.

 

Just for anyone finding this thread and having problems with php & ldap, try this script:

 

<?php

$ldap_server = "ldap://yourserver.domain.suffix"; // put your server and domain here e.g. server1.work.com
$auth_user = "user@domain.suffix"; // e.g. administrator@work.com
$auth_pass = "password";

// Set the base dn to search the entire directory.

$base_dn = "DC=domain, DC=suffix"; //put yoru domain and suffix here eg: "DC=work, DC=suffix"

// Show only user persons
$filter = "(&(objectClass=user)(objectCategory=person)(cn=*))";

// Enable to show only users
// $filter = "(&(objectClass=user)(cn=$*))";

// Enable to show everything
// $filter = "(cn=*)";

// connect to server

if (!($connect=@ldap_connect($ldap_server))) {
     die("Could not connect to ldap server");
}

// bind to server

if (!($bind=@ldap_bind($connect, $auth_user, $auth_pass))) {
     die("Unable to bind to server");
}

//if (!($bind=@ldap_bind($connect))) {
//     die("Unable to bind to server");
//}

// search active directory

if (!($search=@ldap_search($connect, $base_dn, $filter))) {
     die("Unable to search ldap server");
}

$number_returned = ldap_count_entries($connect,$search);
$info = ldap_get_entries($connect, $search);

echo "The number of entries returned is ". $number_returned."<p>";

for ($i=0; $i<$info["count"]; $i++) {
   echo "Name is: ". $info[$i]["name"][0]."<br>";
   echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
   echo "Email is: ". $info[$i]["mail"][0]."<br>";
   echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";
}
?>

Link to comment
Share on other sites

  • 6 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.