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

时间:2016-12-31 01:30:40

标签: 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 = 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!";
    }
         */ 
?>

1 个答案:

答案 0 :(得分:1)

更好的方法是这个:

  <?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!";
}      
?>