2015-06-20 203 views
0

如何使用php进行LDAP身份验证使用PHP进行LDAP身份验证

我成功连接了xampp中的ldap服务器。

的LDAP进行绑定成功

,但在做这个代码的一些错误显示

<?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 a valid username'); 
    } 

    if (!$ldapConnection = @ldap_connect($ldapServer)) { 
     die('Could not connect to ldap server'); 
    } 

    if ([email protected]_bind($ldapConnection, $ldapUsername, $ldapPassword)) { 
     die('Could not bind to 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 get ldap entry'); 
     } 

     $distinguishedName = $ldapEntry[0]['distinguishedname'][0]; 

     if (empty($distinguishedName)) { 
      die('Account information not found'); 
     } 

     if([email protected]_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>'; 
} 

?> 

这是错误显示

Could not complete ldap search

任何提示来解决这个问题

+0

使用“尾-f /无功/日志/ dirsrv /接入” –

+0

SRY我正在期运用窗口广告,这样怎么可以把访问日志从LDAP服务器这是可能的?? – Senthilkumar

回答

0

在LDAP搜索您将不得不使用有效的LDAP过滤器。如果用户名是'johndoe',那么你现在只需要搜索'johndoe'。这就像做以下SQL搜索:

SELECT * FROM users WHERE 'johndoe'; 

你必须给LDAP服务器有机会知道在哪里SERCH为“”“JOHNDOE”“”。因此,您应该使用类似sAMAccountName=johndoe(对于ActiveDirectory)或uid=johndoe(对于OpenLdap)。

对于更深入的例子来看看https://gist.github.com/heiglandreas/5689592

+0

sry不解决plz解释.. – Senthilkumar

+0

当你从'''''''''''''''''''''ldap_search'''中移除'''@'''时,是否收到任何错误消息? – heiglandreas

+0

使用你的例子我应该声明 $ host ='ip'; $ port ='port';像这样 – Senthilkumar