Flatfile login system.

Hello all, i'm making the move from ASP to PHP, especially since I moved from window to linux. Anyway, I'm looking at writing a flat-file login system, unfortunatley, i'm stumped.  The actual login data file would look like this:


and then the next user and so on and so forth. So far, i'm reading each user into an array, like so:

$filename = "accounts";
$struser = explode("[USER=",fread(fopen($filename,"r"), filesize($filename)));

echo $struser[1];
echo "<br />";
echo $struser[2];

The next step, I guess, would be to get the line "PASSWORD=" and see if it corresponds to the relevant username entered into the form. This is where I loose it ;P, any help would be GREATLY appreciated!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$handle = fopen($filename,"r");
$struser = explode("\n", $handle);

try something like this
Give this a shot, calling the authenticateUser() function will return an array of user data if successful, or null and echo an error if it fails.  You could change it around so it returns the error message instead, or sets an error string passed by reference, or just get rid of the echo statements entirely.

function authenticateUser($username, $password) {
      $fp = fopen('data.txt', 'r');
      $out = null;
      $data = array();
      while (!feof($fp)) {
            $line = trim(fgets($fp, 4096));
            if (substr($line, 0, 1)=='[') {
                  $line = substr($line, 1, strlen($line) - 2);
            $index = strpos($line, '=');
            $key = substr($line, 0, $index);
            $value = substr($line, $index+1);
            if ($key=='USER') {
                  if (@$data['USER']==$username) {
                        if ($data['PASSWORD'] != $password) {
                              // login failed
                              echo "Your username and password did not match!";
                              $out = false;
                        } else {
                              // login succeeded
                              $out = $data;
                  } else {
                        $data = array();
            $data[$key] = $value;
      if ($out === null) echo "No username by that name exists";
      return $out;

authenticateUser('Dave', 'password');


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaveHopeAuthor Commented:
Thanks VERY much, I dont think i'd have ever got that by myself!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.