dongqi3533 2015-07-27 15:12
浏览 24
已采纳

PHP登录表单(无sql) - 根据文本文件记录验证 - 未验证

I'm working on a log in form that users a text file (users.txt) to validate username/password against. I cannot use MYSQL for this.

The text file records are in this format:

user one:user1@email.com:user1:password1

user two:user2@email.com:user2:password2

If it validate just the USERNAME only, then it successfully checks the user: if ($currUser == $userUsername) {$valid = true; break;}BUT if I then try to validate both username and password I get the wrong result.($currUser == $userUsername && $currPass == $userPass) {$valid = true; break;} Results in "Invalid username or password"

I can't figure out what I'm doing wrong? When I echo the username and passwords they are a match!!!

SCRIPT:

if(isset($_POST['submit'])){
   $form_is_submitted = true;

   //FORM PROCESSING
   if(isset($_POST['userName']) && isset($_POST['password'])) {

      $currUser = $_POST['userName'];
      $currPass = $_POST['password'];
      $valid = false;//flag
      while (!feof($fileHandle)) {
          $userRow = fgets($fileHandle);
          $userDetails = explode(':', $userRow);
          if (!isset($userDetails[2])) {
              $userDetails[2] = null;
          }
          if (!isset($userDetails[3])) {

              $userDetails[3] = null;
          }
          $userUsername = $userDetails[2];
          $userPass = $userDetails[3];
          if ($currUser == $userUsername /*&& $currPass == $userPass*/) {
              $valid = true;
              //break;
          }
      }    
      if ($valid) {
         echo "<br> $userUsername logged in sucessfully<br>";
      } else {
         echo "<br>Invalid user name or password<br>";
         //FOR DEGUGGING ONLY!
         echo $currUser . $userUsername;
         echo $currPass . $userPass;
         echo $_POST['password'];
         echo $_POST['userName'];

      }
  } else {    
     $errors_detected = true;
     $errors['not set'] = "Please enter username and password";
  }

}
  • 写回答

1条回答 默认 最新

  • dragon5006 2015-07-27 15:24
    关注

    the fgets() function returns a line INCLUDING the linefeed (and the carriage return if its there). that means you have to remove those.

    just change this:

    $userPass = $userDetails[3];
    

    to this:

    $userPass = trim($userDetails[3]);
    

    and it should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案