Well, the only line you actually check is the last one - all the others get read into the $array array, but then you throw immediately throw them away when you read the next line.
Check before going on to the next line of the file, if you find a match, set a $match variable to true (set it to false before you start the loop) and break;. (When you find a match, you don't want to waste your time searching through the rest of your file, hence the break).
Once out of the loop, check $match to see if it's true (at the point where you're currently checking $array). If it's true then a match was found.
That said, you still seem to be a bit confused about what your $username is. I guess you're actually wanting to explode() $line.
$match = false;
$file = file('users.dat');
foreach ($file as $line) ) {
$array = explode(':', $line);
if($array[0]==$username && $array[2]==$password) // You don't really mean $array[1], do you?
{
$match=true;
$break;
}
}
if($match)
{logged in}
else
{not logged in}
if ( ( $array[0] == $username ) && ( $array[1] == $password ) ) {