Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 防止接受空白字段_Php - Fatal编程技术网

Php 防止接受空白字段

Php 防止接受空白字段,php,Php,我正在尝试修复代码中的一个错误,其中空白字段将被接受并允许访问a1user.php页面。所以我添加了一种我认为是验证它的方法,检查存储字段输入的变量是否为空。在测试时,我提交了空白字段,页面花了很长时间加载,最后抛出了警告框。但后来我在我的页面上得到了一个警告,并不断地向错误和警报框发送垃圾邮件。我想我是错误地创建了一个循环,但我不知道该怎么做,因为这显然不起作用!:( 这是我的密码: <?php if ($_SERVER["REQUEST_METHOD"] == "POST") {

我正在尝试修复代码中的一个错误,其中空白字段将被接受并允许访问a1user.php页面。所以我添加了一种我认为是验证它的方法,检查存储字段输入的变量是否为空。在测试时,我提交了空白字段,页面花了很长时间加载,最后抛出了警告框。但后来我在我的页面上得到了一个警告,并不断地向错误和警报框发送垃圾邮件。我想我是错误地创建了一个循环,但我不知道该怎么做,因为这显然不起作用!:(

这是我的密码:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   $Username1 = trim(htmlspecialchars($_POST["user"]));
   $Password1 = trim(htmlspecialchars($_POST["pwd"]));

   $file = fopen("user_accounts.txt", "r") or exit("Unable to open file");

   while(!feof($file))
   {
      $Username2 = trim(fgets($file));
      $Password2 = trim(fgets($file));

      if($Username1 == "" || $Password1 == ""){
         echo '<script language="javascript">';
         echo 'alert("Error - fields blank: Please enter username and password")';
         echo '</script>';
      }
      else
      {
         if($Username1==$Username2 and $Password1 == $Password2)
         {
            header('Location: a1user.php');
         }
         else
         {
            if($Username1=="admin@SNC" and $Password1=="pass1234")
            {
               header('Location: a1admin.php');
            }
         }
      }
      fclose($file);
   }
}
?>

像这样尝试您的代码

第一个更改:我将
$username1=''
$password1=''
的测试从while循环中移出,而不是对代码中的每一行重复测试和警报

2rd更改:我移动了这个:
fclose($file);
在while循环之外,您第一次进入while循环时关闭了文件句柄,导致了无限循环

第3个更改:在
header
函数之后添加
die()
,告诉php如果header函数之后仍然有任何代码,则停止执行

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   $Username1 = trim(htmlspecialchars($_POST["user"]));
   $Password1 = trim(htmlspecialchars($_POST["pwd"]));
   if($Username1 == "" || $Password1 == ""){
     echo '<script language="javascript">';
     echo 'alert("Error - fields blank: Please enter username and password")';
     echo '</script>';
  }
  else {

      $file = fopen("user_accounts.txt", "r") or exit("Unable to open file");

      while(!feof($file))
      {
      $Username2 = trim(fgets($file));
      $Password2 = trim(fgets($file));

         if($Username1==$Username2 and $Password1 == $Password2)
         {
            header('Location: a1user.php');
            die();
         }
         else
         {
            if($Username1=="admin@SNC" and $Password1=="pass1234")
            {
               header('Location: a1admin.php');
               die();
            }
         }
     }
     fclose($file);
     }
 }
 ?>

为什么要使用While循环?如果Username和Password=>为空,则转到特殊页面;如果Username和Password=>Admin,则转到管理面板不存储纯文本密码!为什么要使用heck htmlspecialchars($_POST[“pwd”]))?您可能希望将
user_accounts.txt
文件存储在文档根目录之外,否则任何人都可以简单地请求该文件以获取凭据。我的导师告诉我使用while循环来检查字段中的输入是否等于.txt文件中存储的登录详细信息。我需要空白字段来抛出错误或者,不要去一个特殊的页面。我知道它不安全,但这是我的导师让我做的作业。我还在学习。它不会被用在一个合适的网站上,纯粹用于教育目的。我正在努力学习和理解基础知识,并按照导师的要求去做。我确实理解它不安全,我确信增加的安全性会起作用我尝试了你的建议,它抛出了一个错误:解析错误:语法错误,意外“@charlotterose23关闭php,将
?>
放在
fclose($file)之后}
更新了我的答案谢谢,我刚才也注意到了这一点。这是一个半工作的atm,当我提交空白字段时,页面会刷新,但它只是白色的,没有加载任何内容。警报框也不会显示。@charlotterose23更新了我的答案,删除了
窗口。位置
东西和
死亡()
之后,由于表单位于同一页上,因此不需要它们。。。
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   $Username1 = trim(htmlspecialchars($_POST["user"]));
   $Password1 = trim(htmlspecialchars($_POST["pwd"]));
   if($Username1 == "" || $Password1 == ""){
     echo '<script language="javascript">';
     echo 'alert("Error - fields blank: Please enter username and password")';
     echo '</script>';
  }
  else {

      $file = fopen("user_accounts.txt", "r") or exit("Unable to open file");

      while(!feof($file))
      {
      $Username2 = trim(fgets($file));
      $Password2 = trim(fgets($file));

         if($Username1==$Username2 and $Password1 == $Password2)
         {
            header('Location: a1user.php');
            die();
         }
         else
         {
            if($Username1=="admin@SNC" and $Password1=="pass1234")
            {
               header('Location: a1admin.php');
               die();
            }
         }
     }
     fclose($file);
     }
 }
 ?>