与命令PHP一起使用的变量,无法在下面的代码中使用$line变量。。。?

与命令PHP一起使用的变量,无法在下面的代码中使用$line变量。。。?,php,Php,我试图在下面的while循环中使用$line变量,用于$filter变量,但它没有将其写入命令中 为什么会这样 <?php // NEED TO GET THE $line variable OUT of the WHILE loop!!!!! // Get the UID from the login by reading variables.txt... $fh = fopen('varibles.txt','r'); $line = "";

我试图在下面的while循环中使用
$line
变量,用于
$filter
变量,但它没有将其写入命令中

为什么会这样

<?php

// NEED TO GET THE $line variable OUT of the WHILE loop!!!!!

// Get the UID from the login by reading variables.txt...

       $fh = fopen('varibles.txt','r');
       $line = "";
       while ($line = fgets($fh)) {
       }
        fclose($fh); 

    $ldap_dn = "uid=testbind,ou=users,o=test,c=local";
    $ldap_password = "password";

    $ldap_con = ldap_connect("test.test.local");

    ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

    if(ldap_bind($ldap_con, $ldap_dn, $ldap_password)) {
        $filter = "(uid=$line)";
        $result = ldap_search($ldap_con,"o=test,c=local",$filter) or exit("Unable to search");
        $entries = ldap_get_entries($ldap_con, $result);
        echo $entries[0]['cn'][0]."\n";
    } else {
        echo "Invalid user/pass or other errors!";
    }
         */ 
?>

更好的方法是:

  <?php

   $fh = fopen('varibles.txt','r');
   $line = "";
   while (!feof($fh)) {
      $line = $line = fgets($fh);
      echo $line."<br>";

   }
    fclose($fh); 

$ldap_dn = "uid=testbind,ou=users,o=test,c=local";
$ldap_password = "password";

$ldap_con = ldap_connect("test.test.local");

ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

if(ldap_bind($ldap_con, $ldap_dn, $ldap_password)) {
    $filter = "(uid=$line)";
    $result = ldap_search($ldap_con,"o=test,c=local",$filter) or exit("Unable to search");
    $entries = ldap_get_entries($ldap_con, $result);
    echo $entries[0]['cn'][0]."\n";
} else {
    echo "Invalid user/pass or other errors!";
}      
?>

您有
while($line=fgets($fh)){}
,它只是将行一行一行地放在
$line
变量中,而不做任何处理。感谢您的回复,$filter=“(uid=$line)”;=这条线路仍然失败。我收到此错误。$filter=“(uid=$line)”;=这条线路仍然失败。我得到这个错误。注意:第34行的/var/www/bootstrap/www/ldap\u cn\u filter.php中未定义的偏移量:0这是第34行:echo$entries[0]['cn'][0]。“\n”;