2014-02-14 131 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定義,如果你要使用它。最簡單的方法是簡單地在函數頂部定義變量;這樣它將始終在整個功能中可用。