0

In the following program the first strpos() function is working just just fine when searching for a matching email address in the text file as long as it's on the first line but the second entry it jumps streight to the else clause and echos "Unknown user name" even though that email is on line to of the file. Any ideas?

<?php

$email = $_POST['email'];
$psswd = $_POST['psswd'];

echo "The email address is $email<br>";
echo "The password is $psswd<br><br>";

$file = fopen("./accounts.txt", 'r+') or die("Failed to open file");

while(!feof($file))
{
    $line = fgets($file);
    if(strpos($line, "$email") !== false)
    {
        echo "There was a match";
    if(strpos($line, "$psswd") !== false)
    {
        echo "<br>Password is a match.";

        $db = new SQLite3('./users.db', SQLITE3_OPEN_READWRITE);
            if(!$db)
        {
        echo "Could not open/access DB";
        }
        else
        {
            echo "<br>DB is good!";
        }

        break;
    }
    else
    {
        echo "<br>Invalid Password!";
    }

    }
    else
    {
        echo "Unknown user name";
    //header("localhost/ET/password/:unknown.html");
    break;
    }

    //echo "$line<br>";
}
//$line = fgets(preg_match($email, '$file'));


fclose($file);

?>
2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by pixelsoul
0

The problem is each line in your file is returned by fgets as a string. The strpos stops on the first line/string.

If you were to use file_get_contents it returns the file as one string rather.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.