2015-11-28 154 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) 

:)