从LDAP检索所有结果

时间:2014-04-28 13:42:58

标签: php ldap

我需要一种方法来从Ldap中检索所有用户的列表。我需要的只是他们的名字,以便在页面中显示它们。

这是我的ldap连接页面,用于检索有关一个用户的信息。我需要这个+所有用户的名字。

代码:

session_start();

include 'includes/connect.php';

// Set the ldap server
$ldapurl = "bpt.kme.intern";
// Set the debug flag
$debug = true;

// Set debugging
if ($debug) {
  ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
}

// connect to ldap server
$ldapconn = ldap_connect($ldapurl) or die ("Couldn't connect"); 


$ldapuser= $_POST['email'];
$ldappass= $_POST['pass'];
$ldaptree    = "OU=BPT_Users,DC=bpt,DC=kme,DC=intern";

echo $ldapuser;
echo $ldappass;


// binding to ldap server
//echo "Trying to bind with $ldapuser - $ldappass\n";
$ldapbind = @ldap_bind($ldapconn, $ldapuser, $ldappass);

if (!$ldapbind) {
echo "Unable to bind to server $ldapurl\n";
echo "OpenLdap error message: " . ldap_error($ldapconn) . "\n";
$erro = "Dados de Login Inválidos";
$_SESSION['erro']=$erro;
header('location:index.php');


}



if ($ldapbind) {


     $result = ldap_search($ldapconn,$ldaptree, "(mail=".$ldapuser.")") or die ("Error in search query: ".ldap_error($ldapconn));

     //$data = ldap_get_entries($ldapconn, $result);
      // SHOW ALL DATA
     //echo '<h1>Dump all data</h1><pre>';
    //print_r($data);   
     //echo '</pre>';

    $entry = ldap_first_entry($ldapconn, $result);
    //Title
    $title = ldap_get_values($ldapconn, $entry, "title");
    //Manager
    $manager = ldap_get_values($ldapconn, $entry, "manager");
    //ACC name
    $samaccountname = ldap_get_values($ldapconn, $entry, "samaccountname");
    //Name
    $nome = ldap_get_values($ldapconn, $entry, "name");


    $_SESSION['logged']=true;
    $_SESSION['email'] = $ldapuser;
    $_SESSION['pass'] = $ldappass;
    $_SESSION['title']=$title;
    $_SESSION['manager']=$manager;
    $_SESSION['colab']=$samaccountname;
    $_SESSION['nome']=$nome;


}

提前致谢

1 个答案:

答案 0 :(得分:0)

您正在寻找的是用于检索所有用户的正确LDAP过滤器。这取决于条目的外观 - 试图找出它们的共同点。现在,我想所有用户都有一个mail属性并共享objectClass organizationalPerson。在这种情况下,检索所有条目,如下所示:

$result = ldap_search($ldapconn,$ldaptree, "(&(mail=*)(objectClass=organizationalPerson))") or die ("Error in search query: ".ldap_error($ldapconn));

然后继续,或者在您的示例中尝试,或使用ldap_next_entry方法。