PHP Strong Password

///PHP Strong Password

PHP Strong Password

If you want to prompt your users to create password that is a certain length more or less, that is also strong by today’s definition then here is a good example. This can be elaborated on even more but for common use in most practices this is more than enough. You can comment out like I have in the example below one or more if-else statements if you wish to not enforce that particular type. In my example I commented out the need for symbols like @ # $ &… What this function out of the box seeks is at least 1 letter and 1 number, it seeks to make sure at least 1 letter is capitalized and wants the password to be more than 8 char long but less than 20. This function returns true/false. False if the password has passed the requirements true if not. An example of its use would be\r\n\r\n
\r\nif(pwStrongCheck($_POST['password'] !== false){ echo "Error: Password does not meet strength requirements"; }\r\n
\r\n	public function pwStrongCheck($pwd)\r\n	{\r\n		$error = false;\r\n		if(strlen($pwd) < 8)//to short\r\n		{\r\n			$error = true;\r\n		}\r\n		if(strlen($pwd) > 20)//to long\r\n		{\r\n			$error = true;\r\n		}	\r\n		if(!preg_match("#[0-9]+#", $pwd))//at least one number\r\n		{\r\n			$error = true;\r\n		}\r\n		if(!preg_match("#[a-z]+#", $pwd))//at least one letter\r\n		{\r\n			$error = true;\r\n		}\r\n		if(!preg_match("#[A-Z]+#", $pwd))//at least one capital letter\r\n		{\r\n			$error = true;\r\n		}\r\n		/*if(!preg_match("#\W+#", $pwd))//at least one symbol\r\n		{\r\n			$error = true;\r\n		}*/\r\n		return $error;\r\n	}\r\n
By | 2014-01-05T10:23:15+00:00 March 8th, 2012|Code, PHP|1 Comment

About the Author:

Not much to know about me, I'm a 35+ year old coder, geek, gamer..

One Comment

  1. Pushpinder Bagga December 27, 2012 at 1:17 pm

    Thanks mate – this helped!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: