【问题标题】:PHP error if same username is entered如果输入相同的用户名,PHP 错误
【发布时间】:2015-12-18 23:04:34
【问题描述】:

用户名和密码存储在一个文件中,如果用户名已经存在于文件中,它应该告诉用户他们不能使用该密码并重新加载页面。现在它没有这样做,它直接进入 cmetsWall.php,即使我输入了相同的用户名。我错过了什么?

<html>
<body>
<h1>Please enter your information to create a new login account</h1>
 <form method="post">
  Login Name:<input type = "text"  name = "name" value = "" required><br/>
  Password:<input type = "password" name = "pwd" value = "" required><br/>
  <input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
 </form>
</body>
</html>

<?php
 session_start();
 if($_POST){
  $handle = fopen("accounts.txt", "r");
  if(isset($_POST['name'])){
     $username = $_POST['name'];
     $file = file_get_contents('accounts.txt');
     $search = $username . ",";
     if(strpos($file, $search) !== FALSE){
        echo 'name already used, choose another';
        header('Location: '.$_SERVER['PHP_SELF']);
     }
    fclose($handle);
  }
  if(isset($_POST['pwd'])){
     $password = $_POST['pwd'];
  }
  $text = $username . "," . $password . "\n";
  $fp = fopen("accounts.txt", 'a+') or die("Unable to open file!");
  if(fwrite($fp, $text)){
     echo "saved";
     fclose($fp);
     header("Location: commentWall.php");
  }
  else{
     echo "Unable to store credentials.";
     header('Location: '.$_SERVER['PHP_SELF']);
  }
}
?>

如果你想看cmetsWall.php的代码就问吧,不过我觉得没必要包含。

【问题讨论】:

  • HTML 输出后不能调用session_start()。把它放在最上面。
  • 不幸的是仍然无法正常工作
  • 你应该使用数据库。您是否在accounts.txt 上设置了权限,因此并非所有用户都可以访问它?您可能应该在 HTML 之前拥有所有这些代码。
  • @user3572515 这不是一个解决方案,而是一个不相关的评论。我已经在答案中发布了解决方案。
  • A db 是一个明显的改进,但这只是一个简单的 php 分配,所以我认为没有必要。我没有设置权限,应该是什么?

标签: php forms passwords


【解决方案1】:

至少你需要重构你的代码,这样你就不会发送登录表单,除非没有 POST,或者登录失败。

<?php
 session_start();
 if($_POST){
  $handle = fopen("accounts.txt", "r");
  if(isset($_POST['name'])){
     $username = $_POST['name'];
     $file = file_get_contents('accounts.txt');
     $search = $username . ",";
     if(strpos($file, $search) !== FALSE){
        echo 'name already used, choose another';
        header('Location: '.$_SERVER['PHP_SELF']);
     }
    fclose($handle);
  }
  if(isset($_POST['pwd'])){
     $password = $_POST['pwd'];
  }
  $text = $username . "," . $password . "\n";
  $fp = fopen("accounts.txt", 'a+') or die("Unable to open file!");
  if(fwrite($fp, $text)){
     echo "saved";
     fclose($fp);
     header("Location: commentWall.php");
  }
  else{
     echo "Unable to store credentials.";
     header('Location: '.$_SERVER['PHP_SELF']);
  }
} else {
?>
<html>
<body>
<h1>Please enter your information to create a new login account</h1>
 <form method="post">
  Login Name:<input type = "text"  name = "name" value = "" required><br/>
  Password:<input type = "password" name = "pwd" value = "" required><br/>
  <input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
 </form>
</body>
</html>
<?php
}
?>

【讨论】:

  • 虽然这会生成格式错误的 html - 在 html 标记之外回显内容 ~ 尽管 header 函数调用无论如何都会重定向所以可能不是问题
【解决方案2】:

检测到名称正在使用后,脚本不会停止。你需要做的:

if (strpos($file, $search) !== FALSE) {
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}

另外,如果您已经产生了输出,header() 将不起作用。您应该将顶部的 HTML 块移动到 if ($_POST) 测试的 else 子句。

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 2017-11-12
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多