This article introduces a piece of code implemented using php to detect the password strength. For more information, see. In php programming, especially when users register such a module, there are no more requirements for user password strength. Currently, many websites detect the password strength to obtain the user password that meets the security requirements. However, some password strength detection functions are built on js or other scripts, which may be damaged by malicious attackers who bypass the password detection. This section of code is used to check the length, special characters, numbers, and letters. In addition, you can add additional characters to enhance the security of the password. Check the code as follows:
= 8 & $ length <= 15) {$ strength + = 1 ;} /*** check if lenth is 16-35 chars ***/if ($ length> = 16 & $ length <= 35) {$ strength + = 2 ;} /*** check if length greater than 35 chars ***/if ($ length> 35) {$ strength + = 3 ;} /*** get the numbers in the password ***/preg_match_all ('/[0-9]/', $ password, $ numbers ); $ strength + = count ($ numbers [0]);/*** check for special chars ***/preg_match_all ('/[|! @ # $ % & * \/= ?,;.: \-_ + ~ ^ \\\]/', $ Password, $ specialchars); $ strength + = sizeof ($ specialchars [0]); /*** get the number of unique chars ***/$ chars = str_split ($ password); $ num_unique_chars = sizeof (array_unique ($ chars )); $ strength + = $ num_unique_chars * 2;/*** strength is a number 1-10; ***/$ strength = $ strength> 99? 99: $ strength; $ strength = floor ($ strength/10 + 1); return $ strength ;} /*** call example ***/$ password = 'php _ tutorials_and_examples! 123 '; echo testPassword ($ password);?> |