1. 云栖社区>
  2. PHP教程>
  3. 正文

密码规则验证

作者:用户 来源:互联网 时间:2017-12-01 14:47:12

密码验证规则

密码规则验证 - 摘要: 本文讲的是密码规则验证, function verify(){$letter = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'g'=>7,'h'=>8,'i'=&



function verify(){
$letter = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'g'=>7,'h'=>8,'i'=>9,'j'=>10,'k'=>11,'l'=>12,'m'=>13,'n'=>14,'o'=>15,'p'=>16,'q'=>17,'r'=>18,'s'=>19,'t'=>20,'u'=>21,'v'=>22,'w'=>23,'x'=>24,'y'=>25,'z'=>26);
$letters = array('A'=>1,'B'=>2,'C'=>3,'D'=>4,'E'=>5,'F'=>6,'G'=>7,'H'=>8,'I'=>9,'J'=>10,'K'=>11,'L'=>12,'M'=>13,'N'=>14,'O'=>15,'P'=>16,'Q'=>17,'R'=>18,'S'=>19,'T'=>20,'U'=>21,'V'=>22,'W'=>23,'X'=>24,'Y'=>25,'Z'=>26);
$number = array(0,1,2,3,4,5,6,7,8,9);
$pwd = urldecode($_REQUEST['pw']);
//中文与空格
preg_match_all("/[/x{4e00}-/x{9fa5}/s?]+/u",$pwd,$illegal);
if( ! empty($illegal[0])){
return "非法字符串含有中文或空格";
}
//密码长度限制
if(strlen($pwd) < 6 || strlen($pwd)>16){
return "密码位数必须在6-16个字符之间";
}
//匹配接受字符
preg_match_all("/[/w~!@#$%^&*.]/Uis",$pwd,$allstr);
$allstr_len = strlen($pwd);
if(count($allstr[0]) != $allstr_len){
return "密码不能包含~!@#$%^&*.这外的特殊字符";
}
//9位数以下纯数字处理
preg_match_all("/[0-9]{6,8}/u",$pwd,$nums);
if($nums[0][0] == $pwd){
return "密码不能是9位以下纯数字";
}
preg_match_all("/[0-9]{6,16}/u",$pwd,$match_num);
if($match_num[0][0] == $pwd){
//连续递增,递减数字过滤
$nums= str_split($pwd);
//连续字符密码 123456789
foreach ($nums as $key => $value) {
if($number[$nums[$key+1]]>=$number[$value] && $number[$nums[$key+1]]-1 == $number[$value]){
$ascs[] = $value;
}elseif( ! isset($number[$nums[$key+1]])){
$ascs[] = $value;
}
}
if(count($nums) == count($ascs)){
return '密码不能由连续数字组成';
}
//连续字符密码 987654321
foreach ($nums as $k => $v) {
if($number[$nums[$k+1]]<=$number[$v] && $number[$nums[$k-1]]-1 == $number[$v]){
$descs[] = $v;
}elseif( ! isset($number[$nums[$k-1]])){
$descs[] = $v;
}
}
if(count($nums) == count($descs)){
return "密码不能由连续数字组成";
}
}
//密码不能是相同的字符组成
$a = array($pwd);
foreach($a as $v) {
$t = array_count_values(str_split($v));
$res[$v] = max($t);
}
if($res[$pwd] == strlen($pwd)){
return"密码不能是相同的字符组成";
}
//三个连续字符重复 abcabc
$ex3 = str_split($pwd,3);
$step_len_ex3 = strlen($pwd)/3;
$step_len_param_ex3 = 1;
foreach ($ex3 as $key3 => $value3) {
if($ex3[($key3+1)] == $value3){
$step_len_param_ex3 = $step_len_param_ex3 + 1;
}
}
if($step_len_param_ex3 == $step_len_ex3){
return "密码不能过于简单";
}
//两个连续字符重复 ababab
$ex2 = str_split($pwd,2);
$step_len = strlen($pwd)/2;
$step_len_param = 1;
foreach ($ex2 as $key2 => $value2) {
if($ex2[($key2+1)] == $value2){
$step_len_param = $step_len_param + 1;
}
}
if($step_len_param == $step_len){
return "密码不能过于简单";
}//纯字小写母密码
preg_match_all("/[a-z]{6,16}/u",$pwd,$match);
if($match[0][0] == $pwd){
//连续字符密码 abcdefg
$asc = str_split($pwd);
foreach ($asc as $key => $value) {
if($letter[$asc[$key+1]]>=$letter[$value] && $letter[$asc[$key+1]]-1 == $letter[$value]){
$ascs[] = $value;
}elseif( ! isset($letter[$asc[$key+1]])){
$ascs[] = $value;
}
}
if(count($asc) == count($ascs)){
return '密码不能由连续字符组成';
}
//连续字符密码 gfedcba
$desc = str_split($pwd);
foreach ($desc as $k => $v) {
if($letter[$desc[$k+1]]<=$letter[$v] && $letter[$desc[$k-1]]-1 == $letter[$v]){
$descs[] = $v;
}elseif( ! isset($letter[$desc[$k-1]])){
$descs[] = $v;
}
}
if(count($desc) == count($descs)){
return "密码不能由连续字符组成";
}
}
//纯字大写母密码
preg_match_all("/[A-Z]{6,16}/u",$pwd,$matchs);
if($matchs[0][0] == $pwd){
//连续字符密码 ABCDEFG
$asc = str_split($pwd);
foreach ($asc as $key => $value) {
if($letters[$asc[$key+1]]>=$letters[$value] && $letters[$asc[$key+1]]-1 == $letters[$value]){
$ascs[] = $value;
}elseif( ! isset($letters[$asc[$key+1]])){
$ascs[] = $value;
}
}
if(count($asc) == count($ascs)){
return '密码不能由连续字符组成';
}
//连续字符密码 GFEDCBA
$desc = str_split($pwd);
foreach ($desc as $k => $v) {
if($letters[$desc[$k+1]]<=$letters[$v] && $letters[$desc[$k-1]]-1 == $letters[$v]){
$descs[] = $v;
}elseif( ! isset($letters[$desc[$k-1]])){
$descs[] = $v;
}
}
if(count($desc) == count($descs)){
return "密码不能由连续字符组成";
}
}
}


以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索密码 , 验证 规则 ,以便于您获取更多的相关知识。