codeigniter PHP密码验证不应包含用户名和全名

nfs0ujit  于 2022-12-07  发布在  PHP
关注(0)|答案(2)|浏览(110)

我正在验证密码。所有其他的验证工作正常。我必须在密码上添加首字母验证。密码不应该包含全名或用户名和用户名和全名的第一个/最后一个字符。我试图用strpos函数得到它,但它不工作。这是我的代码,我尝试了2种方法,但不工作。

<?php

$name = "katlina john smith";
$uname = "testinguser";
$password = "john";
$password = "kjs@123"; // first initials of name
$password = "testing@ps"; //These password format should not be accepted

if (strpos($password,$name) !== false || strpos($password,$uname) !== false) {
    echo 'Username found in the password';
}

//2nd way

if (preg_match("#(($uname)|($name))#", $password))
{
    
     echo 'Username found in the password';
}
xfyts7mz

xfyts7mz1#

以下是完整答案

$name = "katlina john smith";
$uname = "testinguser";
$password = "john";
if ( (strpos($name,$password) !== false ) || ( strpos($uname,$password ) !== 
false ) ) {
 echo 'password not valid';
 }
c9qzyr3d

c9qzyr3d2#

使用此选项:

$name = "katlina john smith";
$username = "testinguser";
$password = "katlina@test123";

对于PHP 7:

if (strpos(strtolower($password), strtolower($username)) === false || passContainsName($name, $password)) {
    echo 'password not valid in PHP 7'.PHP_EOL;
 }
 
 
 function passContainsName($name, $password){
     foreach (explode(" ", $name) as $toCheck) {
         if (strpos(strtolower($password), strtolower($toCheck)) === false) {
            return true;
         }
     }
     return false;
 }

对于PHP 8:

if (str_contains(strtolower($password), strtolower($username)) || passContainsName($name,$password)) {
 echo 'password not valid';
 }
 
 
 function passContainsName($name, $password){
     foreach (explode(" ", $name) as $toCheck) {
         if (str_contains(strtolower($password), strtolower($toCheck))) {
            return true;
         }
     }
     return false;
 }

在这里摆弄php 7和8的例子1

相关问题