yes, the logic is at fault. The while() loops are executed sequentially and the if block is evaluated after both have completed, so the script is comparing elements from the last line of each file. You'll need to nest those while loops or better yet, read the elements into arrays and compare them.
I don't mean to cramp your style, but there are a few points I feel I should mention.
Again, I don't mean to cramp your style or make you uncomfortable, but these tips help a lot of people.