2015-11-28 126 views
1

我将以下代码的一部分发布到了另一个问题,以解决我遇到的问题,并找到了解决方案。我可以从我的Android设备注册,登录和注销,这很好。但是,现在我得到了一些奇怪的东西。我添加了一个额外的功能,以验证$ user_pass和$ confirm密码变量是否相等(比较字符串)。不过,我总是会得到一个json消息“密码不匹配”。php用于密码验证

if($userPass!==$confirmPass){ 


     $don = array('result' =>"fail","message"=>"Passwords don't match"); 

} 

整个代码如下。

<?php 
    session_start(); 
    require "init.php"; 
    header('Content-type: application/json'); 

    $email = $_POST['email']; 
    $user_name = $_POST['user_name']; 

    $user_pass = $_POST['user_pass']; 
    $passwordEncrypted = sha1($user_pass); 

    $confirmPass = $_POST['confirm_pass']; 
    $confPasswordEncrypted = sha1($confirmPass); 

    $msg = "Congratulations. You are now registered to the most amazing app ever!";    

     if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ 


      $don = array('result' =>"fail","message"=>"Please enter a valid email"); 

     } 




     if($email && $user_name && $user_pass && $confirmPass && filter_var($email, FILTER_VALIDATE_EMAIL)){ 


     $sql_query = "select * from user_info WHERE email ='".mysqli_real_escape_string($con, $email)."' or user_name 
     ='".mysqli_real_escape_string($con, $user_name)."'"; 

     $result = mysqli_query($con, $sql_query); 

     $results = mysqli_num_rows($result); 

     if ($results){ 
      $don = array('result' =>"fail","message"=>"Email or username exists."); 
     }else{ 

      $sql_query = "insert into user_info values('$email','$user_name','$passwordEncrypted','$confPasswordEncrypted');"; 

      if(mysqli_query($con,$sql_query)){ 

       $don = array('result' =>"success","message"=>"Successfully registered!Well done"); 
       //mail($email,"Well done",$msg); 
       $_SESSION['email']=$row['email']; 
      } 
     } 
    } if(!$email){ 


     $don = array('result' =>"fail","message"=>"Please enter your email");     


    }else if(!$user_name){ 


     $don = array('result' =>"fail","message"=>"Please enter your username"); 


    }else if(!$user_pass){ 


     $don = array('result' =>"fail","message"=>"Please enter a password"); 

    //The last if statement causes the problem. 
    }else if($userPass!==$confirmPass){ 


     $don = array('result' =>"fail","message"=>"Passwords don't match"); 

    } 


     echo json_encode($don); 

?> 
+1

使用'如果($为userpass!= $ confirmPass){',而不是'如果($为userpass !== $ confirmPass){'并检查它是否工作。 –

+0

仍然遇到同样的问题。 – Theo

+1

也许他们真的不一样?你尝试'var_dump($ userPass,$ confirmPass)'? –

回答

1

尝试使用的

if(!strcmp($userPass,$confirmPass)) 

代替

if($userPass!==$confirmPass) 
1

固定它。我有一个额外的parenthensis在

if($userPass!==$confirmPass)) 

,所以我把它改成

if($userPass!==$confirmPass) 

:)