Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ldap authentication

Status
Not open for further replies.

gogirl

MIS
Jun 5, 2002
46
0
0
US
Hello,

Can someone please look at my search.php code and tell me how to authenticate to an ldap server. The code I am using will allow me to see info on the ldap server. However, I can not figure out how to search and authenticate. Any help would be greatly appreciated.

Note: There is a form page with a username, password, and submit button that uses the search.php page as its action. I want to be able to type in username(cn) and password to log in and authenticate to ldap server.

<html>
<head>
</head>
<body>
<?php
/*
** ldap_list example
** This script explores the OUs at CTW
** Links are created to explore units within units
*/

if(!isset($dn))
{
$dn = &quot;ou=Floor09,o=CTW&quot;;
}

print(&quot;<B>Search DN:</B> $dn<BR>\n&quot;);

//connect to LDAP server
if(!($ldap=ldap_connect(&quot;localhost&quot;)))
{
die(&quot;Could not connect to LDAP server!&quot;);
}

$filter = &quot;objectClass=*&quot;;
$attributes = array(&quot;cn&quot;, &quot;mail&quot;, &quot;givenname&quot;, &quot;surname&quot;, &quot;telephonenumber&quot;);
$query = &quot;(&(cn=&quot; . $POST['cn'] . &quot;))&quot;;

//perform search
if(!($result = ldap_search($ldap, $dn, $filter, $attributes)))
{
die(&quot;Nothing Found!&quot;);
}

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

for($i = 0; $i < $entries[&quot;count&quot;]; $i++)
{
if(!$entries[$i][&quot;mail&quot;][0])
{
}
else
{
//print($entries[$i][&quot;dn&quot;]);
//print(&quot;&nbsp&quot;);
print($entries[$i][&quot;cn&quot;][0]);
print(&quot;&nbsp&quot;);
print($entries[$i][&quot;mail&quot;][0]);
print(&quot;&nbsp&quot;);
print($entries[$i][&quot;givenname&quot;][0]);
print(&quot;&nbsp&quot;);
print($entries[$i][&quot;surname&quot;][0]);
print(&quot;&nbsp&quot;);
print($entries[$i][&quot;telephonenumber&quot;][0]);
}

print(&quot;<BR>\n&quot;);

}

ldap_free_result($result);

//close ldap connection
ldap_close($ldap);

?>

</body>
</html>

Thanks!
 
Thanks ViperSB but I think that it has more to do with the code. I am able to see attributes from the directory. I just don't know what code to use to authenticate.

 
Use ldap_bind()

// using ldap bind
$ldaprdn = 'id=username,dc=server,dc=com'; // ldap rdn or dn
$ldappass = 'password'; // associated password

// connect to ldap server
$ldapconn = ldap_connect(&quot;ldap.example.com&quot;)
or die(&quot;Could not connect to LDAP server.&quot;);

if ($ldapconn) {
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
// verify binding
if ($ldapbind) {
echo &quot;LDAP bind successful...&quot;;
} else {
echo &quot;LDAP bind failed...&quot;;
}
}

Viper_SB
Mod at LDAP section
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top