【问题标题】:PHP 2 dimensional array undefined offset errorPHP 2维数组未定义偏移错误
【发布时间】:2020-02-17 04:17:15
【问题描述】:

我是 php 新手,我创建了一个网页来输入用户名和密码,然后将其与存储的列表进行比较以授予或拒绝访问该站点。我的代码可以正常运行,但我不断收到未定义的偏移错误。

<html lang="en">
<head>
    <title>Insecure System</title>
</head>
<body>
    <?php
        // read file and set text to string
        $myFile = fopen("includes/users.txt","r");
        $content = fread($myFile,filesize("includes/users.txt"));
        fclose($myFile);

        // set string to two dimensional array
        $contentArray = explode("||>><<||",$content);
        foreach ($contentArray as $cArr) {
            $contentArray2[] = explode(",",$cArr);
        }
        //print_r($contentArray2);

        // retrieve user input values
        if (isset($_POST['user']) && isset($_POST['pass'])) {
            $enteredUser = $_POST['user'];
            $enteredPass = $_POST['pass'];


            // if statements to check user/pass combo
            for ($i=0;$i<=sizeof($contentArray2);$i++) {
                for ($i2=0;$i2<=sizeof($contentArray2[$i]);$i2++) {
                    if ($contentArray2[$i][$i2] == $enteredUser) {
                        $i2++;
                        if ($contentArray2[$i][$i2] == $enteredPass) {
                            echo "Access Granted";
                            break 2;
                        }
                        else {
                            echo "Access Denied"; 
                            break 2;
                        }
                    }
                    else continue;
                }
            }
        }
    ?>
    <form action="index.php" method="post">
        <input type="text" placeholder="Username" name="user">
        <input type="text" placeholder="Password" name="pass">
        <input type="submit" value="Submit">
        <input type="reset" value="Reset">
    </form>
</body>
</html>

users.txt 的内容:

beavis,password||>><<||butthead,password2||>><<||dana,alien||>><<||fox,believe

产生的错误输出:未定义的偏移量:第 29 行 index.php 中的 2

第29行指代码中的这一行:if ($contentArray2[$i][$i2] == $enteredUser) {

$contentArray2 的值​​:Array ( [0] =&gt; Array ( [0] =&gt; beavis [1] =&gt; password ) [1] =&gt; Array ( [0] =&gt; butthead [1] =&gt; password2 ) [2] =&gt; Array ( [0] =&gt; dana [1] =&gt; alien ) [3] =&gt; Array ( [0] =&gt; fox [1] =&gt; believe ) )

【问题讨论】:

  • 请打印 $contentArray2 的值​​
  • $i&lt;=sizeof($contentArray2)$i2&lt;=sizeof($contentArray2[$i]) 应该是 $i&lt;sizeof($contentArray2)$i2&lt;sizeof($contentArray2[$i])
  • @SterlingH:不用担心 :)
  • 您是否告诉 Beavis 和 Butthead 您将他们的密码以纯文本形式存储在 txt 文件中?所有这些关于密码泄露的新闻报道通常只是泄露密码的“散列”。但是您将其作为纯文本存储在任何人都可以访问的 txt 文件中。想想你在做什么。如果您在欧洲,如果发现这一点,GDPR 将给您带来很多罚款。
  • @Andreas,网页的标题可能会泄露一点&lt;title&gt;Insecure System&lt;/title&gt;

标签: php html arrays multidimensional-array


【解决方案1】:

for loops 中都使用&lt; 而不是&lt;=


            // if statements to check user/pass combo
            for ($i=0; $i < sizeof($contentArray2); $i++) {
                for ($i2=0; $i2 < sizeof($contentArray2[$i]); $i2++) {
                    // your other code
                }
            }

【讨论】:

    【解决方案2】:

    虽然您已经解决了您的问题,但我想展示一种替代方法。

    主要思想是不要做那么多中间步骤。在阅读文件的同时,为什么不检查一下用户名和密码呢?

    其他几件事 - 使用 file_get_contents() 作为 fopen/fread/fclose 的简写,并且仅在您打算使用该文件时才读取该文件(在主 if 内)

    if (isset($_POST['user']) && isset($_POST['pass'])) {
        $enteredUser = $_POST['user'];
        $enteredPass = $_POST['pass'];
    
        //read file and set text to string
        $content = file_get_contents("a.csv");
    
        // set string to two dimensional array
        $users = [];
        foreach ( explode("||>><<||",$content) as $user )   {
            list($name, $password) = explode(",", $user, 2);
            // If the user matches
            if ($enteredUser == $name )  {
                // Output depending on password matching
                if ($enteredPass == $password) {
                    echo "Access Granted";
                }
                else {
                    echo "Access Denied";
                }
                // Exit loop as checked user
                break;
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      你可以试试

      <html lang="en">
          <head>
              <title>Insecure System</title>
          </head>
          <body>
              <?php
                  // read file and set text to string
                  $myFile = fopen("users.txt","r");
                  $content = fread($myFile,filesize("users.txt"));
                  fclose($myFile);
      
                  // set string to two dimensional array
                  $contentArray = explode(" ",$content);
      
                  foreach ($contentArray as $key => $cArr) {
                      $contentArray2[] = explode("||>><<||",$cArr);
                  }
      
                  if (isset($_POST['user']) && isset($_POST['pass'])) {
                      $enteredUser = $_POST['user'];
                      $enteredPass = $_POST['pass'];
                      $flag = 0;
                      foreach($contentArray2 as $value){
                          if(trim($value[0]) == trim($enteredUser) && trim($value[1]) == trim($enteredPass)){
                              echo "Access Granted";
                              break;
                          }else{
                              $flag = 1;
                          }
                      }
      
                      if($flag != 0){
                          echo 'Denied'; die;
                      }
                  }
              ?>
              <form action="" method="post">
                  <input type="text" placeholder="Username" name="user">
                  <input type="text" placeholder="Password" name="pass">
                  <input type="submit" value="Submit">
                  <input type="reset" value="Reset">
              </form>
          </body>
          </html>
      

      将此 (shariful||&gt;&gt;&lt;&lt;||123456 kabir||&gt;&gt;&lt;&lt;||12345678 jack||&gt;&gt;&lt;&lt;||987654) 文本放入 users.txt 文件中

      【讨论】:

        【解决方案4】:

        您还可以使用用户名、密码创建一个关联数组,以简化授予权限。

        $myFile = fopen("includes/users.txt","r");
        $content = fread($myFile,filesize("includes/users.txt"));
        fclose($myFile);
        
        // set string to two dimensional array
        $contentArray = explode("||>><<||",$content);
        foreach ($contentArray as $cArr) {
             $temp = explode(",",$cArr);
             $contentArray2[$temp[0]] = $temp[1];
        }
        //print_r($contentArray2);
        
        // retrieve user input values
        if (isset($_POST['user']) && isset($_POST['pass'])) {
             $enteredUser = $_POST['user'];
             $enteredPass = $_POST['pass'];       
        
             if($contentArray2[$enteredUser] === $enteredPass){ 
                 echo "Access Granted";
             }else{
                 echo "Access denied";
             }
        }
        

        或者甚至更简单。在 username,password 上做一个 in_array

        $myFile = fopen("includes/users.txt","r");
        $content = fread($myFile,filesize("includes/users.txt"));
        fclose($myFile);
        
        // retrieve user input values
        if (isset($_POST['user']) && isset($_POST['pass'])) {
             $enteredUser = $_POST['user'];
             $enteredPass = $_POST['pass'];       
        
             if(in_array($enteredUser . "," . $enteredPass, $content)){ 
                 echo "Access Granted";
             }else{
                 echo "Access denied";
             }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多