Dan Solo
Dan Solo

Reputation: 717

PHP string comparison results in Undefined Offset 3 error

I'm trying to compare a couple of array values stored in a flat file (I know, mysql is much better but its an assigment so please bear with the antiquated methodology) with some form sent user credentials from a $_POST variable and despite there only being one line of data in the file I keep getting the following error:

Notice: Undefined offset: 3 in ./login.php on line 70

I've included the offending lines of code below with some of the preamble and the printed out results from the browser (in a basic attempt at trouble shooting I've run a print_r command and clearly get 2 arrays returned but can't figure out why...?).

If I can add any additional info that would help solve it please let me know - I'll be only too willing to oblige.

The user registration data is captured in this fragment of code from a register.php file

<?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);
}
?>

Then the checking of user input login details happens in this fragment from the login.php file

<?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>';
        }
    }
}
?>

The output from this file after submitting the login details is as follows:

Array ( [0] => )

Notice: Undefined offset: 3 in ./login.php on line 70

Array ( [0] => Test User [1] => 123 Shallot Street, The Big Onion [2] => BN21 0LK [3] => testuser [4] => password )

Password and Username match - Thank you for logging in.

Upvotes: 2

Views: 2270

Answers (1)

Kai Qing
Kai Qing

Reputation: 18843

If you refer to this php reference:

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

You see the example shows a key value reference like so:

$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>';
    }
}

see if that helps any.

also, am I misreading this? It looks like you deliberately add a new line:

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

why not get rid of that \n

Upvotes: 1

Related Questions