【问题标题】:PHP string comparison results in Undefined Offset 3 errorPHP 字符串比较导致 Undefined Offset 3 错误
【发布时间】:2012-01-10 00:14:51
【问题描述】:

我正在尝试比较存储在一个平面文件中的几个数组值(我知道,mysql 要好得多,但它是一个分配,所以请忍受过时的方法)与某种形式从 $_POST 变量发送的用户凭据尽管文件中只有一行数据,但我不断收到以下错误:

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

我在下面包含了一些有问题的代码行以及一些前导码和浏览器打印的结果(在基本的故障排除尝试中,我运行了一个 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] => )

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

数组([0] => 测试用户 [1] => 123 Shallot Street, The Big Onion [2] => BN21 0LK [3] => testuser [4] => 密码)

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

【问题讨论】:

    标签: php


    【解决方案1】:

    如果你参考这个 php 参考:

    http://php.net/manual/en/function.file.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>';
        }
    }
    

    看看有没有帮助。

    还有,我是不是看错了?看来您是故意添加新行:

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

    为什么不摆脱那个\n

    【讨论】:

    • 恐怕没有变化 - 仍然出现同样的错误。我不明白为什么如果文件只包含一行数据,我会从 print_r 获得 2 个输出,尽管第一行非常不完整。
    • 我更新了我的答案。看起来您在分隔字符串之前添加了一个新行。 "\n" 我假设这是在有多行的情况下,它会将它们添加到自己的行中。您可以将其放在字符串的末尾,然后忽略explode不返回大于1的数组的情况,或者任何数量的更好的方法来补偿尾随\ n ...就像关闭之前的基本substr文件...等很多方法来做到这一点。
    • 毕竟在php.net/manual/en/function.file.php 页面上找到了答案。出现这种情况是因为我使用了 '\n' 方法来输入每一行新的数据,我还需要将以下内容添加到我的文件命令中以忽略任何空行,这将解释 print_r 输出中的空数组。 $lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);感谢您朝正确的方向轻推。
    • 谢谢 - 刚刚看到您的编辑和评论 - 这两个选项似乎达到了相同的最终结果。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2021-12-25
    相关资源
    最近更新 更多