I'm adapting a script for an admin panel so it allows multiple users but I'm having a problem with the coding for writing to a file when one of the users changes their password. When it writes to the file, the first character of the second line of the file is left off.
if(isset($_POST['newpassword'])) {
$changedpass = $_POST['newpassword']; //put md5 back in later
$fb = file("./tpassword.txt");
$f = fopen( "tpassword.txt", 'r+');
fseek($fb,0,SEEK_SET);
ftruncate($fb,0);
foreach($fb as $line)
{
@list($id,$passwd,$su) = explode('|', $line);
if ($id == $_COOKIE['person']) //checks id for match with current logged in user
{
fputs ($f, $id."|".$changedpass."|".$su); //replaces the password with the new one
}
else
{
fputs ($f, $line); //all other lines written to file.
}
fclose ($f);
}
echo "<script>window.location=\"testadmin.php?act=logout\"</script>"; //redirects to logout
}
The output in the file that I get is:
mike|tokyo|0
hief|chief|0
capn|capn|0
instead of
mike|tokyo|0
chief|chief|0
capn|capn|0
The username comes first, the password second. The 0 refers to a feature that hasn't been implemented yet.
The following code was used to write the first line of the original file:
if(isset($_GET['act']) && $act=="new") {
$username = $_POST['uname'];
$password = $_POST['pass']; //put md5 back in
$fp = fopen("tpassword.txt", 'w');
fwrite ($fp,$username."|".$password."|0\n");
fclose ($fp);
chmod("tpassword.txt", 0777);
echo "<script>window.location=\"testadmin.php\"</script>";
break;
}
and the following code was used for the subsequent lines:
if(isset($_GET['act']) && $_GET['act'] == "newuser") {
if(isset($_POST['newuser']) && isset($_POST['newuserpass'])) {
$fr = file("./tpassword.txt");
foreach ($fr as $line) {
$thisline = explode("|", $line);
if ($thisline[0] == $_POST['newuser']) {
include ("./header.php");
echo "Username already in use<br>";
echo "<p><center><a href=\"testadmin.php?act=newuser\">Try again</a>";
include ("./footer.php");
die();
}
}
$fd = fopen("./tpassword.txt","a+");
fputs ($fd, $_POST['newuser']."|".$_POST['newuserpass']."|0\n"); //put md5 back in for newuserpass
fclose($fd);
echo "<script>window.location=\"testadmin.php\"</script>";
}
The passwords will eventually be hashed using md5() but for testing purposes are unhashed hence the comments
It is most perplexing. I'm quite new to all this and I'm learning as I go along so any help would be greatly appreciated.