LDAP Authentication with PHP -


how ldap authentication php

i connect ldap server in xampp.

the ldap bind successful

but working on code error shows

<?php       $ldapbasedomain = 'ou=employee,dc=domainname';     $ldapserver = 'domainname';     $ldapusername = 'xxx';     $ldappassword = 'yyy';   if (!empty($_post['username']) && !empty($_post['password'])) {       if (!preg_match('/^[a-za-z0-9\-]+$/', $_post['username'])) {         die('please enter valid username');     }      if (!$ldapconnection = @ldap_connect($ldapserver)) {         die('could not connect ldap server');     }      if (!@ldap_bind($ldapconnection, $ldapusername, $ldappassword)) {         die('could not bind ldap server');     }      if (!$ldapsearch = @ldap_search($ldapconnection, $ldapbasedomain, $_post['username'])) {         die('could not complete ldap search');     }      $ldapcount = @ldap_count_entries($ldapconnection, $ldapsearch);      if (!$ldapcount) {         die('account not found');     } else {         if (!$ldapentry = @ldap_get_entries($ldapconnection, $ldapsearch)) {             die('could not ldap entry');         }          $distinguishedname = $ldapentry[0]['distinguishedname'][0];          if (empty($distinguishedname)) {             die('account information not found');         }          if(!@ldap_bind($ldapconnection, $distinguishedname, $_post['password'])) {             die('password incorrect');         }          echo '  <h1>logged in successfully</h1>                 <h2>user details</h2>';          echo '<pre>' . print_r($ldapentry[0], true) . '</pre>';     }  } else {     echo '  <form method="post">             username: <input name="username"><br>             password: <input name="password" type="password"><br>             <input type="submit" value="login">             </form>'; }  ?> 

this error shows

could not complete ldap search

any hints solve problem

in ldap-search have use valid ldap-filter. if username 'johndoe' searching 'johndoe'. it's doing following sql-search:

select * users 'johndoe'; 

you have give ldap-server chance know serch '''johndoe'''. therefore should use samaccountname=johndoe (for activedirectory) or uid=johndoe (for openldap).

for more indepth example have @ https://gist.github.com/heiglandreas/5689592


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -