Эта функция выполняет то, что вы просили, с некоторыми дополнительными функциями, которые упростят ее в будущем, если вы захотите расширить свой код, например, обновить базу данных, что намного проще, чем хранить в текстовом файле.
Этот ответ предполагает, что ваш data.txt
файл имеет следующую структуру:
user1.email@site.com password user2.email@site.com otherpassword
вот код:
<?php
//Your variables
$email = $_POST['email'];
$old_password = $_POST['password'];
$new_password = $_POST['new_password'];
//Call the function to change the password
changePassword( $email, $old_password, $new_password );
function changePassword( $email, $old_password, $new_password){
//The status will be updated to true if successful.
//This function returns boolean.
$status = false;
//The new password can't have spaces
if( strpos( $new_password, " " ) !== false ){
return $status;
}
//Read the file.
$file = fopen( "data.txt", "r+", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );
$read = fread( $file, filesize( "data.txt" ));
//Close the file
fclose( $file );
//Explode the data. There are two values which make up a user, so chunk into users.
$data = array_chunk( explode( " ", $read ), 2 );
//The names of each element (purely to make code easier)
$keys = array(
"email",
"password"
);
//For every user in the data file.
foreach( $data as $userID=>$user ){
//Combine the user data with keys
$user = array_combine( $keys, $user );
//If the current credentials are correct
if( $user["email"] == $email && $user["password"] == $old_password ){
//Change the password on this user
$user["password"] = $new_password;
//Update the user record
$data[$userID] = $user;
//We have changed the user password, change status
$status = true;
//Since we updated the user, we don't need to proceed through the loop
break 1;
}
}
//This is the output data
$outputData = array();
//Since we broke the array into users, we need to put it back into a 1d array
foreach($data as $userID=>$user){
$outputData[] = implode( ' ', $user );
}
//Implode the array so we can put it back in the file
$outputData = implode( " ", $outputData );
//Write the data to the file.
file_put_contents( "data.txt", $outputData );
//Return the status, which will be true.
return $status;
}