PHP 函数停止执行 - 没有错误

标签 php mysql function passwords syntax-error

我今天调试了一些 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/

相关文章:

php - GAPI : Failed to authenticate user. 永久修复 PHP

java - 具有 ON DUPLICATE KEY UPDATE 的插入查询的 Statement 类中的executeUpdate(String sql) 方法

php - 自动将 SQL 表内容发送到电子邮件地址

C++ std::sort() - 无法对访问成员变量的类类型的 vector 进行排序

swift - 函数在调用后执行

php - 长轮询 PHP。是否保持数据库连接?

php - 在 Laravel 5.5 中测试授权策略时遇到问题

JavaScript 在函数完成之前继续!? Ajax异步混淆及解决方法

php - 防止 div 重新加载

mysql - 将成功的 MySQL 查询转化为更新