Heim  >  Artikel  >  Backend-Entwicklung  >  Detailliertes Beispiel, wie PHP die Stärke eines Passworts bestimmt

Detailliertes Beispiel, wie PHP die Stärke eines Passworts bestimmt

怪我咯
怪我咯Original
2017-07-11 11:53:212408Durchsuche

Bei der

-Website handelt es sich um eine Test-Website, die Benutzern dabei helfen kann, die Sicherheit des von ihnen festgelegten Passworts und die zum Knacken benötigte Zeit zu testen. Der Zweck besteht darin, Benutzern mitzuteilen, dass sie das Passwort nicht auf a festlegen sollen einfach 123456, aber die Einbeziehung von Zahlen, Buchstaben, Sonderzeichen usw. verbessert die Sicherheitsfunktionen von Passwörtern. In diesem Artikel wird hauptsächlich die Verwendung von PHP zur Bestimmung der Stärke von Passwörtern vorgestellt. Die Beispiele lauten wie folgt:

1. PHP-Seite

$score = 0;
if(!empty($_GET['value'])){ //接收的值
    $str = $_GET['value'];
} else{
    $str = '';
}
if(preg_match("/[0-9]+/",$str))
{
    $score ++;
}
if(preg_match("/[0-9]{3,}/",$str))
{
    $score ++;
}
if(preg_match("/[a-z]+/",$str))
{
    $score ++;
}
if(preg_match("/[a-z]{3,}/",$str))
{
    $score ++;
}
if(preg_match("/[A-Z]+/",$str))
{
    $score ++;
}
if(preg_match("/[A-Z]{3,}/",$str))
{
    $score ++;
}
if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]+/",$str))
{
    $score += 2;
}
if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]{3,}/",$str))
{
    $score ++ ;
}
if(strlen($str) >= 10)
{
    $score ++;
}
echo $score;
exit;

2. HTML-Seite

<table cellspacing="0" cellpadding="0">
<tr>
<td>输入密码:</td>
<td colspan="4"><input type="password" value="" name="newpwd" onblur="getPassword();" />
</tr>
<tr>
<td>密码强度:</td>
<td id="idSM1" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT1" style="DISPLAY: none">弱</span></td>
<td id="idSM2" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT0" style="DISPLAY:inline; FONT-WEIGHT: normal; COLOR: #666">无</span><span id="idSMT2" style="DISPLAY: none">中等</span></td>
<td id="idSM3" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT3" style="DISPLAY: none">强</span></td>
<td id="idSM4" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"> <span style="height:0px; line-height:0px;"> </span><span id="idSMT4" style="DISPLAY: none">极好</span></td>
</tr>
</table>

3. js

<script>
function getPassword(){
    var value = $("input[name=&#39;newpwd&#39;]").attr(&#39;value&#39;);
    $.get(&#39;index.php?r=account/testpwd&#39;,{value:value},function(data){
        if(data>=1 && data<=3){
            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon1&#39;); //弱
            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);
            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);
            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);
            $(&#39;#idSMT1&#39;).show();
            $(&#39;#idSMT0&#39;).hide();
            $(&#39;#idSMT2&#39;).hide();
            $(&#39;#idSMT3&#39;).hide();
            $(&#39;#idSMT4&#39;).hide();
        } else if(data>=4 && data<=6){ //中等
            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon2&#39;);
            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon2&#39;);
            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);
            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);
            $(&#39;#idSMT0&#39;).hide();
            $(&#39;#idSMT1&#39;).hide();
            $(&#39;#idSMT2&#39;).show();
            $(&#39;#idSMT3&#39;).hide();
            $(&#39;#idSMT4&#39;).hide();
        } else if(data>=7 && data<=8){ //强
            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon3&#39;);
            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon3&#39;);
            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon3&#39;);
            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);
            $(&#39;#idSMT0&#39;).hide();
            $(&#39;#idSMT1&#39;).hide();
            $(&#39;#idSMT2&#39;).hide();
            $(&#39;#idSMT3&#39;).show();
            $(&#39;#idSMT4&#39;).hide();
        } else if(data>=9 && data<=10){ //极好
            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);
            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);
            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);
            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);
            $(&#39;#idSMT0&#39;).hide();
            $(&#39;#idSMT1&#39;).hide();
            $(&#39;#idSMT2&#39;).hide();
            $(&#39;#idSMT3&#39;).hide();
            $(&#39;#idSMT4&#39;).show();
        }
    });
}

4

Das obige ist der detaillierte Inhalt vonDetailliertes Beispiel, wie PHP die Stärke eines Passworts bestimmt. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn