2012-01-10 122 views
2

我试图比较存储在一个平面文件中的几个数组值(我知道,mysql更好,但它的分配,所以请忍受过时的方法)与一些形式发送从$ _POST变量的用户凭据,尽管仅存在是一个数据行的文件中我不断收到以下错误:PHP字符串比较结果未定义偏移3错误

注意:未定义抵消:3 ./login.php线70

我已经在下面列出了一些有问题的代码行,其中包括一些序言和从浏览器打印出来的结果(在进行故障排除的基本尝试中,我运行了一个print_r命令,并清楚地得到了2个返回的数组,但无法计算出结果在外面HY ...?)。

如果我可以添加任何有助于解决问题的其他信息,请让我知道 - 我只愿意承担义务。

用户注册数据在这个代码片段从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] =>)

注意:未定义偏移量:3 ./login.php线70上

阵列([0] =>测试用户[1] => 123葱街,大洋葱[2] => BN21 0LK [3] => testuser的[4] =>密码)

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

回答

1

如果你参考这个PHP参考:

http://php.net/manual/en/function.file.php

你。请参阅示例显示ak ey值参考如下:

$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

+0

没有改变我害怕 - 仍然得到同样的错误。我不明白为什么如果该文件只包含一行数据,我会从print_r中获取2个输出,尽管这是非常不完整的第一行。 – 2012-01-10 00:28:21

+1

我更新了我的答案。它看起来像在分隔字符串之前添加一个新行。 “\ n”我认为这是为了防止有多条线路将它们添加到自己的线路中。你可以在字符串末尾抛出它,然后忽略爆炸没有返回大于1的数组的情况,或者在关闭之前补偿尾部的\ n ...像一个基本的substr的任何数量的更好的方法文件...等多种方式来做到这一点。 – 2012-01-10 00:34:37

+0

毕竟在http://php.net/manual/en/function.file.php页面上找到了答案。 它出现是因为我使用'\ n'方法输入每一个新的数据行,我还需要添加以下内容到我的文件命令来忽略任何空行,这将解释print_r输出中的空数组。 $ lines = file($ filename,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 感谢您在正确的方向推动。 – 2012-01-10 00:39:26