In this page, you learn to create a simple password checker application in PHP. The password checker is able to determine whether a password input by the user is strong or weak. A strong password must be more than eight characters and contain alphabetical characters, symbols, and digits. The order of these items appeared in the password text is not important.
This application represents an implementation of the Regular Expression in PHP. In the password checker application, you need to define three separate string patterns for the preg_match function. The first pattern will be used to match the alphabetical characters (a to z or A to Z). The second pattern will match symbols. And the last pattern will match digits. You wil call the preg_match function three times since you have three different patterns. If every returning value is equal to 1, the password is strong. Otherwise, it is a weak password. The page of the password checker application looks like the following.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
$patalpha='/[a-zA-Z]+/'; //match characters a to z or A to Z
$patdigit='/\d+/'; //match digits
$patsym='/\W+/'; //match symbols
$f=preg_match($patalpha,$pwd) && preg_match($patdigit,$pwd) && preg_match($patsym,$pwd);
<form method="post" action="passwordchecker.php">
<table style="background-color: #CCC">
<input type="text" name="txtuser" />
<input type="password" name="txtpwd" />
if($strong==1) echo "<p style='color:#0000ff'>Strong </p>";
else echo "<p style='color:#ff0000'>weak </p>";
<input type="submit" name="btsubmit" value="submit" />
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: