我将以下代码的一部分发布到我遇到的问题的另一个问题,并找到了解决方案。我可以从我的Android设备注册,登录和注销,这很好。但是,现在我变得奇怪了。我添加了一个额外的功能,以验证$ user_pass和$ confirm密码变量是否相等(比较字符串)。但是我总是从这里得到一条json消息“密码不匹配”。
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);
?>
答案 0 :(得分:1)
尝试使用
if(!strcmp($userPass,$confirmPass))
而不是
if($userPass!==$confirmPass)
答案 1 :(得分:1)
修正了它。
我有额外的家长if($userPass!==$confirmPass))
所以我把它改成了
if($userPass!==$confirmPass)
:)
答案 2 :(得分:0)