2

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

No change I'm afraid - still getting the same error. I don't understand why if the file only contains one line of data I'm getting 2 outputs from the print_r, albeit a very incomplete first line.
I updated my answer. It looks like you add a new line before the delimited string. "\n" I assume this is in case there are multiple lines it would add them to their own line. You can throw that at the end of the string and then just ignore cases where the explode does not return an array greater than 1, or any number of better ways to compensate for the trailing \n... like a basic substr before closing the file... etc. many ways to do it.
Found the answer on the php.net/manual/en/function.file.php page after all. It appears because I've used the '\n' method for entry of every new line of data I also needed to add the following to my file command to ignore any empty lines, which would explain the empty array in the print_r output. $lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); Thanks for your nudge in the right direction.
Thanks - just seen your edit and comment - both options seem to achieve the same end result. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.