phpLDAP filter dn & uid
-
21-12-2019 - |
Question
I have the following code:
if($_POST){
$ldap['user'] = $_POST['user'];
$ldap['pass'] = $_POST['pass'];
$ldap['conn'] = ldap_connect('my_ip')
or die("Could not connect to {'my_ip'}" );
ldap_set_option($ldap['conn'], LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap['conn'], LDAP_OPT_REFERRALS, 0);
$ldap['bind'] = @ldap_bind($ldap['conn'], $ldap['user'], $ldap['pass']);
if( !$ldap['bind'] )
{
echo ldap_error( $ldap['conn'] );
exit;
}
And a login form:
<form method="POST" action="">
<p>
User Name:
<input type="text" name="user" >
Password:
<input type="password" name="pass" >
<input type="submit" name="submit" value="Submit">
</p>
</form>
Everything works fine, except, in order to work, I have to input the entire DN as username. Like this: uid=user,ou=people,dc=my-domain,dc=com
.
What I want is to set the base dn and get the uid as the necessary username.
I tried setting $basedn = 'dc=mydomain,dc=de'and $filter="uid"
with $sr=ldap_search($filter, $basedn);
but I don't know how to bind it to my login $_POST variable.
Solution
After very thoroughly searching through the other questions, I found a duplicate with the appropriate answer.
LDAP bind using POST variable for UID?
So, according to zenlord:
$ldap['user'] = 'uid =' . $_POST['user'] . ',ou=XXX,dc=XXX,dc=XXX,dc=XXX';
or
$ldap['user'] = "uid =$_POST['user'],ou=XXX,dc=XXX,dc=XXX,dc=XXX";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow