PHP password checker


PHP password checker

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.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Password Checker</title>
</head>
<body>
<?php
if(isset($_POST['txtpwd']))
{
$pwd=$_POST['txtpwd'];
$strong=check($pwd);
}

function check($pwd){
$f=0;
if(strlen($pwd)>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);
}

return $f;
}
?>

<form method="post" action="passwordchecker.php">
<table style="background-color: #CCC">

<tr>
<td>
User name:
</td>
<td colspan="2">

<input type="text" name="txtuser" />
</td>

</tr>
<tr>
<td>
Password:
</td>

<td colspan="2">
<input type="password" name="txtpwd" />
</td>
<td>

<?php
if(isset($strong)){
if($strong==1) echo "<p style='color:#0000ff'>Strong </p>";
else echo "<p style='color:#ff0000'>weak </p>";
}
?>

</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" name="btsubmit" value="submit" />
</td>
</tr>
</table>
</form>

</body>
</html>

 

password checker

Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.