我今天调试了一些 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";
}
}
这是我从错误检查设置中获得的输出(我的网站主机不会为整个站点启用 php 调试,因此我必须浏览一个单独的文件,稍后我将发布该文件):
4
Mast
4
Mast
{"success":"noerror"}
这是我检查错误的方法:
<?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();
}
}
}
}
}
最佳答案
三元组===
确保返回的类型相同。
在您的函数中,您并不总是返回 bool 值,有时您会返回字符串,这可能是一个问题。
例如这个片段:
$data = "pass_charfail";
if($data){
echo 'true';
}else{
echo 'false';
}
这将回显 true
因为 $data
不是空字符串。
但以下内容将回显 false
,因为 $data
不是 true
bool 值。
$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
类型。
希望这对您有意义!
关于PHP 函数停止执行 - 没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612112/