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

?>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.