I have been working on this all day, and am not getting this to work. I am creating an application where a user would authenticate against Active Directory. Yesterday I was able to get PHP to connect to the AD server and display entries using this script.

<?

// PHP script to connect to the Active Directory Server a return a result
// used for testing Active Directory connections.



$dn = "OU=Staff,OU=LCDC,OU=Anoka-Hennepin,DC=ah,DC=isd11";

    $attributes = array("displayName", "department");

    $filter = "(cn=*)";

    $ad = ldap_connect("ldap://myadserver";)
          or die("Couldn't connect to AD!");

    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);

$bd = ldap_bind($ad, "CN=Tuller\, Mike,OU=Staff,OU=LCDC,OU=Anoka-Hennepin,DC=ah,DC=isd11","password")
or die("Couldn't bind to AD!");


    $result = ldap_search($ad, $dn, $filter, $attributes);

    $entries = ldap_get_entries($ad, $result);

    for ($i=0; $i<$entries["count"]; $i++)
    {
        echo $entries[$i]["displayname"]
             [0].", ".$entries[$i]["department"][0]."<br />";
    }

    ldap_unbind($ad);

?>

Everything lists correctly. I looked on the web, and in the mailing lists, and between the two have come up with this script to authenticate. I have a web page with forms to enter the username and password.


<?

$dn = 'OU=Staff,OU=LCDC,OU=Anoka-Hennepin,DC=ah,DC=isd11';

function ldap_authenticate()
{
    $username = $_POST['username'];
        $password = $_POST['password'];

    if ($username != "" && $password != "")
    {
        if (! ($ad = ldap_connect("172.22.1.20")))
        {
                die("Could not connect to LDAP server!");
        }

        ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);

if (! ldap_bind( $ad, "CN=Tuller\, Mike,OU=Staff,OU=LCDC,OU=Anoka-Hennepin,DC=ah,DC=isd11", $password))
{
die("Unable to bind to server!");
}
if (! ($r = ldap_search( $ad, $dn, 'cn=' . $username)))
{
die("Nothing Found!");
}
if ($r)
{
$result = ldap_get_entries( $ad, $r);
if ($result[0])
{
if (ldap_bind( $ds, $result[0][$dn], $password) )
{
return $result[0];
}
}
}
}
}


if (($result = ldap_authenticate()) == NULL) {
    echo('Authorization Failed');
    exit(0);
}
echo('Authorization success');
print_r($result);

?>

When I run everything though, I get this error:

Warning: ldap_search(): Search: No such object in /Library/Apache2/htdocs/ldap/auth.php on line 23
Nothing Found!


I have looked at this for too long, and now am to the point where I am out of ideas. Could someone look at this and see if they can figure out what I am doing wrong here?


Thanks,

Mike Tuller

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to