【问题标题】:PHP retrieving elements from filePHP从文件中检索元素
【发布时间】:2015-09-10 08:22:44
【问题描述】:

我正在从文本文档中检索信息并尝试测试用户输入是否与文本文档中的任何信息匹配。

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<?php
$userpassfile = fopen("usernamepass.txt", "r") or die("Unable to open file!");
$username = $_POST["usernamepassword"];
$_SESSION['$usernamepassword'] = $_POST["usernamepassword"];
while(!feof($userpassfile)){
    $line = fgets($userpassfile);
    $users = explode("#", $line);
    for($x=0; $x<sizeof($users); $x++){
        if($users[$x]==$username){
            header('Location: booking.php');
        } else {
            header('Location: login.php');
        }
    }   
}
if($username=="ADMINadmin"){
    header('LOCATION: admin.php');
}

fclose($usernamefile);

当它检查txt中的信息时,似乎只检查第一个和最后一个元素。我不知道为什么。我认为这是因为在 for 循环中有一个

【问题讨论】:

  • 这可能是 MS-Windows 和所有其他系统上的行编码不同的问题。这可能会破坏 php 在文件中视为“行”的内容。
  • 作为建议,不要使用文本文件进行用户/密码登录。会有几个危险的安全问题。
  • 这只是一个项目。没有什么会被开发出来。

标签: php for-loop


【解决方案1】:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<?php
$userpassfile = fopen("usernamepass.txt", "r") or die("Unable to open file!");
$username = $_POST["usernamepassword"];
$_SESSION['$usernamepassword'] = $_POST["usernamepassword"];

if($username=="ADMINadmin"){
    header('LOCATION: admin.php');
    fclose($usernamefile);
    exit; // If user is admin, there is no need for further checking.
} else {
    header('Location: login.php'); // Set redirect to login.php by default
}

while(!feof($userpassfile)){
    $line = fgets($userpassfile);
    $users = explode("#", $line);
    if(in_array($username, users) {
       // If user was found, override redirect to login.php
       // and set it to booking.php
       header('Location: booking.php');
       // And there is no need to iterate further, so stop the loop
       break;
    }
}

fclose($usernamefile);

【讨论】:

    【解决方案2】:

    也许你的代码必须是这样的

    <?php
    session_start();
    ?>
        <!DOCTYPE html>
        <html>
    <?php
    $username = $_POST["usernamepassword"];
    $_SESSION['$usernamepassword'] = $_POST["usernamepassword"];
    if($username=="ADMINadmin"){
        header('LOCATION: admin.php');
    } else {
        $userpassfile = fopen("usernamepass.txt", "r") or die("Unable to open file!");
        $in_file = false;
        while(!feof($userpassfile)){
            if($in_file) {
                break;
            }
            $users = explode("#", fgets($userpassfile));
            $in_file = in_array($username, $users);
        }
    
        if($in_file) {
            header('Location: booking.php');
        } else {
            header('Location: login.php');
        }
        fclose($userpassfile);
    }
    

    【讨论】:

      【解决方案3】:
      <?php
          session_start();
          // note here that i has removed your DOCTYPE INPUT ...
      
          $location                       = NULL;
          $defaultPage                    = "login.php";
          $username                       = $_POST["usernamepassword"];
          $_SESSION['$usernamepassword']  = $username;
      
          // this test must be the first ... for performance
          if($username=="ADMINadmin"){
              $location = "admin.php";
          }
          else {
              $userpassfile = fopen("usernamepass.txt", "r") or die("Unable to open file!");
              while(!feof($userpassfile)){
                  $line   = fgets($userpassfile);
                  $users  = explode("#", $line);
                  for($x=0; $x<sizeof($users); $x++){
                      // i have added trim : maybe there is spaces or other invisible chars in your file
                      // note: if your login system is case insensitive so use this condition :
                      //       if(strtolower(trim($users[$x])) == strtolower($username)){ ... }
                      if (trim($users[$x]) == $username){
                          $location = "booking.php";
                          break;
                      } 
                      // here it is the mistake : if the first token of the first line is not the connected user
                      // so php will redirect to login page
                      /*
                      else {
                          header('Location: login.php');
                      }
                      */
                  }   
              }
              fclose($usernamefile);
          }
          // it is a good practice to be explicit... for maintenance simplicity
          if (is_null($location)) {
              $location = $defaultPage;
          }
      
          header("Location: " . $location);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 2012-09-19
        • 2012-04-30
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多