2014-09-01 111 views
0

我今天一直在调试一些php代码,并遇到了一个很奇怪的问题。我必须检查密码是否有效的函数将停止执行部分函数。 PHP或Web服务器本身都不会产生错误。PHP函数停止执行 - 没有错误

这里是有问题的功能:

//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调试整个网站,所以我必须去通过一个单独的文件我将发表从后面的代码):

肥大

肥大

{ “成功”: “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(); 
       } 
      } 
     }  
    } 
} 

回答

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类型的字符串。

希望它对你有意义!

0

我得到了我再次工作,但我不知道为什么我所做的改变有所作为。如果有人可以回应这个答案或张贴自己的答案解释它将不胜感激。

我如何修正它在更改if ($data)行后checkPassword被调用到if ($data === true),它报告了正确的错误消息,而不是声明注册成功。