我今天一直在调试一些PHP代码并遇到了一个非常奇怪的问题。我必须检查密码是否有效的功能停止执行该功能。 PHP或Web服务器本身都不会生成错误。
这是有问题的功能:
//Common Registration Functions
function checkPassword($password)
{
$bLen = strlen($password);
echo $bLen."\n";
echo $password."\n";
//Remove any illegal characters
$vPWord = preg_replace("/[^\!\@\#\\\$\%\&\*\-\_\,\.a-zA-Z0-9]/","",$password);
$aLen = strlen($vPWord);
echo $aLen."\n";
echo $vPWord."\n";
//If the password length before santization is different than after then the user used illegal characters
if ($bLen <> $aLen)
{
return "pass_charfail";
}
echo "pass length check 1 \n";
//Check sanitized password length
if (strlen($vPWord) < 6)
{
return "pass_short";
}
echo "pass length check 2 \n";
if (strlen($vPWord) > 10)
{
return "pass_long";
}
echo "pass length check 3 \n";
//Check password strength
$strength = 0;
if (preg_match("/[^a-z]/",$vPWord))
{
$strength += 1;
}
if (preg_match("/[^A-Z]/",$vPWord))
{
$strength += 1;
}
if (preg_match("/[^0-9]/",$vPWord))
{
$strength += 2;
}
if (preg_match("/[^\!\@\#\\\$\%\&\*\-\_\,\.]/",$vPWord))
{
$strength += 4;
}
if ($strength > 6)
{
echo $strength."\n";
return true;
}
else
{
echo $strength."\n";
return "pass_weak";
}
}
以下是我从错误检查设置中得到的输出(我的webhost不会为整个站点启用php调试,所以我必须通过一个单独的文件,我将在稍后发布代码):
4
肥大
4
肥大
{&#34;成功&#34;:&#34; NOERROR&#34;}
以下是检查错误的方法:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include("register.php");
?>
以下是调用上述函数的函数:
function register($username, $password, $email, $squestion, $sanswer)
{
//First check if the email is valid
$data = eVerify($email);
//If email is not valid
if (!$data)
{
return "email_fail";
}
//If email is valid then check if it already exists and the verification status
else
{
//See if the email already exists
$data = getUID($email,"email",true);
//echo $data."\n";
if ($data)
{
//Get user ID for later use
$id = getUID($email,"email",false);
//If the email exists, see if it has been verified or not
$data = checkVer($id);
//echo $data."\n";
//If email exists but has not been verified
if (!$data)
{
rSVCode($username,$email,$id);
return "exists1";
exit();
}
//If email exists and has been verified
else if ($data)
{
return "exists2";
exit();
}
}
//If email does not exist, continue registration process
else
{
//Check to see if username has been used
$data = getUID($username,"username",true);
if ($data)
{
return "un_exists";
exit();
}
//Check password strength, chars, and length
else
{
$data = checkPassword($password);
if ($data)
{
//Create user account
$data = cAccount($username, $password, $email, $squestion, $sanswer);
if ($data)
{
//Get user's ID for use later
$id = getUID($username,"username",false);
//Generate email verification code
$data = cVCode($username,$email,$id);
//Send verification email
$data = sendEVar($email,$username,$data);
if ($data)
{
return "true";
exit();
}
else
{
return $data;
exit();
}
}
else
{
return $data;
exit();
}
}
else
{
return $data;
exit();
}
}
}
}
}
答案 0 :(得分:1)
三重===
确保返回的类型相同。
在你的函数中,你并不总是返回布尔值,有时你会返回字符串,这可能是一个问题。
例如此代码段:
$data = "pass_charfail";
if($data){
echo 'true';
}else{
echo 'false';
}
这将回显true
,因为$data
不是空字符串。
但是以下内容会回显false
,因为$data
不是true
布尔值。
$data = "pass_charfail";
if($data === true){
echo 'true';
}else{
echo 'false';
}
您的register
功能中的另一个示例
if ($data)
{
return "true";
exit();
}
如果此值返回,则false
将从以下代码回显:
if($data === true){
echo 'true';
}else{
echo 'false';
}
因为$ data现在是一个不是boolean
类型的字符串。
希望对你有意义!
答案 1 :(得分:0)
我让它再次运作,但我不确定为什么我所做的改变会有所作为。如果有人可以回答这个答案或发表他们自己的答案,我们将不胜感激。
我修复它的方法是在将checkPassword调用到if ($data)
后更改if ($data === true)
行,并报告了正确的错误消息,而不是声称注册成功。