I am trying to replace a word in a file. I have a file that works as password database, it hold the md5 encrypted passwords of users, like this:
// User 01 - John
$pass[John] = "md5.hash.here"; // Edited for security obviously ;)
$user[John] = "John";
$group[John] = "admin";
// User 02 - Rob
$pass[Rob] = "none";
$user[Rob] = "Rob";
$group[Rob] = "user";
As you see, the 2nd user does not have a valid md5 hash. I want to give these users the oppurtunity to enter a new password for themselves. I tried the following:
// Chunk of login.php
if($pass[$_POST['user']] == "none") { include('setpass.php'); exit; }
// Chunk of setpass.php
// This opens the password file
$pf = fopen('password.db','w') or die ('Woops, this is wrong.<br>Error code: 104');
// This set the source piece of text, currently in the password file
$sp = $pass[$_SESSION['user']];
$ep = "$sp = \"none\";";
// This should replace the string "none" with the md5'ed password.
$rep1 = "none";
$rep2 = md5($_POST['nwpass']);
$newpass = str_replace($rep1, $rep2, $ep);
// This should write the str_replace to the file
$setnwpass = fwrite($pf, $newpass) or die ('You should not see this.<br>Error code: 105');
fclose($pf);
exit;
}
However, when the user logs in, he does get the prompt that he can change his password, but when something is entered, the password database file is emptied and all it says afterwards is, and I quote:
= "md.hash.here";
What am I overlooking ?