2014-02-14 78 views
-1

********请阅读全文......我想弄清楚如何让LDAP查询工作...我不问为什么我得到一个未定义的变量错误,我只是说这是我在运行代码时遇到的唯一错误。我知道变量需要定义,即使定义了变量,也不会返回结果。我需要帮助查询LDAP与PHP

我正在尝试使用php搜索LDAP。我不需要添加或编辑信息,只需拉取搜索结果即可。我一直在阅读php手册条目和我可以做的例子,但是我仍然有点麻烦。随着我有下面的代码,我得到的唯一错误是

“通知:未定义的变量:在使用ldapsearch()(的 C线42 ad_users:\ Program Files文件 (x86)的\ Zend的\ Apache2的\ htdocs目录\ TLSConnect3App \网站\所有\模块\定制\ TLSConnectPackage \ ldap_search \包括\的functions.php)“。

我也试图找出在哪里可以找到LDAP中的samaccountname。那是uid还是displayName?我的最终目标是能够搜索名/姓或电子邮件并提取用户的照片,姓名,名单和电子邮件。

<?php 
function ldapSearch(){ 
    $ldap_password = 'H******5!!1'; 
    $ldap_username = 'cn=Directory Manager'; 
    $ldaphost = "ldap-server"; 
    $ldapport = 389; 

    $ldapconn = ldap_connect($ldaphost,$ldapport) 
     or die('Could not connect to '.$ldaphost); 

    // We have to set this option for the version of Active Directory we are using. 
    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version'); 
    ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search. 

    if (TRUE === ldap_bind($ldapconn, $ldap_username, $ldap_password)){ 
     $ldap_base_dn = 'DC=,DC=co,DC=uk'; 
     $search_filter = '(&(objectCategory=person)(displayName= Admin))'; 
     $attributes = array(); 
     $attributes[] = 'givenName'; 
     $attributes[] = 'mail'; 
     $attributes[] = 'samaccountname'; 
     $attributes[] = 'sn'; 
     $result = ldap_search($ldapconn, $ldap_base_dn, $search_filter, $attributes); 
     $entries = ldap_get_entries($ldapconn, $result); 
     if (FALSE !== $result){ 
      $entries = ldap_get_entries($ldapconn, $result); 
      for ($x=0; $x<$entries['count']; $x++){ 
       if (!empty($entries[$x]['givenname'][0]) && 
        !empty($entries[$x]['mail'][0]) && 
        !empty($entries[$x]['samaccountname'][0]) && 
        !empty($entries[$x]['sn'][0]) && 
        'Shop' !== $entries[$x]['sn'][0] && 
        'Account' !== $entries[$x]['sn'][0]){ 
         $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array(
          'email' => strtolower(trim($entries[$x]['mail'][0])), 
          'first_name' => trim($entries[$x]['givenname'][0]), 
          'last_name' => trim($entries[$x]['sn'][0])); 
        } 
      } 
     } 
     ldap_unbind($ldapconn); // Clean up after ourselves. 
    } 

$message = "Retrieved ". count($ad_users) ." Active Directory users\n"; 
} 
+0

第42行在哪里?你对“Undefined variable”有什么不了解?其含义非常清楚。 –

+0

我明白这是什么意思。我不是在问这是什么意思,我在问为什么会这样。换句话说,代码中是否有错误?还有一个原因是$ ad_users [strtoupper(修剪($条目[$ X] [ 'Sam帐户'] [0]))] =阵列( \t \t \t \t \t \t \t '电子邮件'=>用strtolower(修剪($项[$ X] [ '邮件'] [0])), \t \t \t \t \t \t \t 'first_name的'=>修剪($条目[$ X] [ '给定名称'] [0]), \t \t \t \t \t \t \t'last_name'=> trim($ entries [$ x] ['sn'] [0]));没有定义它.....如果你看看这个问题,你会看到它是关于LDAP而不是我的错误。我刚才解释说这是我收到的唯一错误。 – slpcc63

回答

0

尝试在函数的开头定义$ad_users = array()

+0

我这样做,它没有帮助。当我打印$ entries数组时,计数为0,所以它看起来不像查询拉动信息。 – slpcc63

0

问题在于$ad_usersif块内部定义,然后在其外部使用。如果跳过if块,则该变量将不会被定义,您将收到通知。

$something = false; 

if ($something == true) { // This will be skipped 
    $myVar = 'It works!'; // And this variable will not be defined 
} 
echo $myVar; // Notice: Undefined variable: myVar 

总之,你需要确保$ad_users定义,如果你要使用它。最简单的方法是简单地在函数顶部定义变量;这样它将始终在整个功能中可用。