The following PHP code instance helps you reset the password. requestResetPassword is used to receive and check the password reset request. For better reusability, I will separately allocate the password reset operation to a new resetPassword function. After changing the password, I will call sendEmail to send a notification email to the user.
Copy codeThe Code is as follows:
/**
* The receiver that the user requests to reset the password
*/
Function requestResetPassword (){
// Check whether the user exists
If (! CheckUserExists ($ _ GET ['userid']) {
Exit ('Sorry, the user does not exist. Please confirm the user account. ');
}
ResetPassword ($ _ GET ['userid']);
// Finally, send an email to the user
SendEmail ($ _ GET ['userid'], 'password reset successfully', and 'New password is XXX ');
Exit ('the new password has been sent to your mailbox. ');
}
/**
* Helping users reset their passwords
*/
Function resetPassword ($ userid ){
// Check whether the user exists
If (! CheckUserExists ($ userid )){
Return false;
}
// Reset the User Password
// Omitted...
Return true;
}
/**
* Send an email to the user
*/
Function sendEmail ($ userid, $ title, $ content ){
// Check whether the user exists
If (! CheckUserExists ($ userid )){
Return false;
}
// Send email
// Omitted...
Return true;
}
/**
* Check whether a user exists
*/
Function checkUserExists ($ userid ){
$ User = getUserInfo ($ userid );
Return! Empty ($ user );
}
/**
* Obtain the data of a user.
*/
Function getUserInfo ($ userid ){
// Assume that I have a query function used to query the database and return data.
$ User = query ("SELECT * FROM 'user' WHERE 'uid' =". intval ($ userid ));
Return is_array ($ user )? $ User: array ();
}
The problem is that the three functions both use the checkUserExists function to check whether the user does not exist and the database queries three times, resulting in some additional overhead.
It seems possible to remove any checkUserExists between the three. However, if some functions require calling resetPassword or sendEmail, an error may occur if the user does not exist.
Another solution is to write the resetPassword logic to requestResetPassword, and then write the sendEmail logic. In this way, the number of function calls is reduced, and the database query becomes one time, thus improving the performance. However, the password reset and email sending functions cannot be reused and violate the single responsibility principle, and the Code complexity is also improved.
However, because function separation and reusability are both good, if the actual performance is affected, you may consider using the cache method to reduce database queries. I modified the checkUserExists functions they share:
Copy codeThe Code is as follows:
/**
* Check whether a user exists
*/
Function checkUserExists ($ userid ){
// Add a cache to record the user's results
Static $ cache = array ();
// Check whether the current user has checked once
If (isset ($ cache [$ userid]) {
Return $ cache [$ userid];
}
$ User = getUserInfo ($ userid );
// Record the result to the cache
$ Cache [$ userid] =! Empty ($ user );
Return $ cache [$ userid];
}
You can also use the same method to change the getUserInfo function.
It can be seen that when code reusability is improved, it is very easy to improve the performance, and the performance bottleneck is easily discovered and modified.
Although this example does not have a big impact on the performance, there are still some greater impacts. For example, traversal may encapsulate traversal into a function for reuse and use it multiple times. These expenses do not have much impact on my project as expected, or are minimal. Therefore, I prefer to spend more time on improving code reusability and maintainability, rather than worrying about wasting more time on this performance. If the actual performance does not meet the requirements, you can also increase the hardware configuration.