Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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字符串比较导致未定义的偏移量3错误_Php - Fatal编程技术网

PHP字符串比较导致未定义的偏移量3错误

PHP字符串比较导致未定义的偏移量3错误,php,Php,我试图将存储在平面文件中的两个数组值(我知道,mysql要好得多,但它是一种辅助方法,因此请注意过时的方法)与从$\u POST变量发送的一些表单用户凭据进行比较,尽管文件中只有一行数据,但我始终得到以下错误: 注意:第70行未定义的偏移量:3 in./login.php 我在下面列出了一些有问题的代码行,以及一些序言和从浏览器打印出来的结果(在解决问题的基本尝试中,我运行了print_r命令,显然返回了2个数组,但不知道为什么…?) 如果我可以添加任何其他信息,将有助于解决这个问题,请让我知道

我试图将存储在平面文件中的两个数组值(我知道,mysql要好得多,但它是一种辅助方法,因此请注意过时的方法)与从$\u POST变量发送的一些表单用户凭据进行比较,尽管文件中只有一行数据,但我始终得到以下错误:

注意:第70行未定义的偏移量:3 in./login.php

我在下面列出了一些有问题的代码行,以及一些序言和从浏览器打印出来的结果(在解决问题的基本尝试中,我运行了print_r命令,显然返回了2个数组,但不知道为什么…?)

如果我可以添加任何其他信息,将有助于解决这个问题,请让我知道-我将非常乐意

用户注册数据在register.php文件的这段代码中捕获

<?php
$filename = '../../private_data/filewriting.php';

if (isset($_POST['register']) && count($errors)==0) {

    // Submission was OK, so display thank-you message
    $output .= '<p><strong>Thanks for registering with us! Please click <a href="index.php">here</a> to login to the site using your Username and Password</strong></p>';

    $handle = fopen($filename, 'a');

    fwrite($handle, "\n".$clean['fullname']."|".$clean['address']."|".$clean['postcode']."|".$clean['username']."|".$clean['password']);

    fclose($handle);
}
?>

然后在login.php文件的这个片段中检查用户输入的登录详细信息

<?php
$output = '';
$filename = '../../private_data/filewriting.php';

if (isset($_POST['submit']) && count($errors)==0) {

    $lines = file($filename);

    foreach ($lines as $line) {
        $arr = explode('|', $line);
        print_r($arr);
        echo '<br />';

        // The next line is the problematic line 70             
        if ($arr[3]==$clean['username'] && $arr[4]==$clean['password']) {
            $_SESSION['username'] = $clean['username'];
            $_SESSION['password'] = $clean['password'];
            $output .= '<p><strong>Password and Username match - Thank you for logging in. Please continue to browse the site using the links above.</strong></p>';
        }
    }
}
?>

提交登录详细信息后,此文件的输出如下:

数组([0]=>)

注意:第70行未定义的偏移量:3 in./login.php

数组([0]=>测试用户[1]=>香葱街123号,大洋葱[2]=>BN21 0LK[3]=>测试用户[4]=>密码)


密码和用户名匹配-感谢您登录。

如果您参考此php参考:

您可以看到该示例显示了一个键值引用,如下所示:

$lines = file($filename);

foreach ($lines as $line_num => $line) {
    $arr = explode('|', $line);
    print_r($arr);
    echo '<br />';

    if ($arr[3]==$clean['username'] && $arr[4]==$clean['password']) {
        $_SESSION['username'] = $clean['username'];
        $_SESSION['password'] = $clean['password'];
        $output .= '<p><strong>Password and Username match - Thank you for logging in. Please     continue to browse the site using the links above.</strong></p>';
    }
}

为什么不摆脱它呢\n

恐怕没有变化-仍然会出现同样的错误。我不明白为什么如果文件只包含一行数据,我会从打印中得到两个输出,尽管第一行非常不完整。我更新了我的答案。看起来您在分隔字符串之前添加了新行。“\n”我假设这是在有多行的情况下,它会将它们添加到自己的行中。您可以在字符串末尾抛出该值,然后忽略explode不返回大于1的数组的情况,或者忽略补偿尾随值的任何更好方法\n。。。就像关闭文件之前的基本substr。。。等等。有很多方法。毕竟在页面上找到了答案。之所以出现这种情况,是因为我使用了“\n”方法来输入每一行新数据,我还需要将以下内容添加到我的file命令中以忽略任何空行,这将解释打印输出中的空数组$行=文件($filename,file_IGNORE_NEW_line | file_SKIP_EMPTY_line);感谢你朝着正确的方向努力。谢谢-刚刚看到你的编辑和评论-两个选项似乎都达到了相同的最终结果。谢谢你的帮助!
fwrite($handle, "\n".$clean['fullname']."|".$clean['address']."|".$clean['postcode']."|".$clean['username']."|".$clean['password']);