Hi All :evilgrin:
I have currently a months training on PHP and currently busy with a Asset Register Project. I have a problems authenticating with Ldap from PHP.
Here is my code
First Form ( LoginPage.php)
<html>
<head>
<title>Login Form</title>
</head>
<body>
<p>Login Form</p>
<!-- create the “main” form with an input text box named pid and a password text box named password -->
<form name="main" method="post" action="authcheck.php">
<table>
<tr>
<td align="right">Username: </td>
<td> <input name="Username" type="text" id="Username"> </td>
</tr>
<tr>
<td align="right">password: </td>
<!-- make sure this is type “password”! -->
<td> <input name="password" type="password" id="password"> </td>
</tr>
<tr>
<td align="center" colspan="2"><input name="btnsubmit" type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Second Page (Authcheck.PHP)
<?php
$host = 'xx.xxxxxxxxxxxxx.net';
$port = 'xxx';
$baseDn = 'dc=xxxxxxxx,dc=net';
$pid = $POST['Username']; // get these values explicitly from the POST
$credential = $POST['Username'];
/ldap will bind anonymously, make sure we have some credentials/
if (isset($Username) && $password != '' && isset($credential)) {
$ldap = ldap_connect($host,$port);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 10);
if (isset($ldap) && $ldap != '') {
/ search for pid dn /
$result = @ldap_search($ldap, $baseDn, 'uuUsername='.$Username, array('dn'));
if ($result != 0) {
$entries = ldap_get_entries($ldap, $result);
$principal = $entries[0]['dn'];
if (isset($principal)) {
/ bind as this user /
if (@ldap_bind($ldap, $principal, $credential)) {
print('Authenticate success');
} else {
print('Authenticate failure');
}
} else {
print('User not found in LDAP');
}
ldap_free_result($result);
} else {
print('Error occured searching the LDAP');
}
ldap_close($ldap);
} else {
print('Could not connect to LDAP at '.$host);
}
}
?>