我有一个脚本来注册用户或检查用户信息是否有任何错误,例如密码不匹配或用户是否已经存在等。
当我打印用户名是否已被占用的错误消息时,$errorMessage 的值始终为 0,我不知道为什么:
<?php
//get user name and passwords
$errorMessage = "Error: ";
$successMessage;
if ($p1 == ""){
$errorMessage = $errorMessage + "You left your password blank. ";
echo $errorMessage;
$fail = $fail + 1;
}
if ($p2 == ""){
$errorMessage = $errorMessage + "You did not type your password in a second time. ";
$fail = $fail + 1;
echo $errorMessage;
}
//hash passwords
$hash1 = sha1($p1);
$hash2 = sha1($p2);
$fail = 0;
//check if user name is taken
if($hash1 != $hash2){
$fail = $fail + 1;
$errorMessage = $errorMessage + "Your passwords do not match. ";
echo $errorMessage;
}
if ($numRows > 0){
$errorMessage = $errorMessage + "That User name is already taken, please try another username. ";
$fail = $fail + 1;
echo $errorMessage;
}
if ($userName == ""){
$errorMessage = $errorMessage + "You left your username blank. ";
$fail = $fail + 1;
echo $errorMessage;
}
if ($email == ""){
$errorMessage = $errorMessage + "You left your email blank. ";
$fail = $fail + 1;
echo $errorMessage;
}
//do not register user if fails
if( $fail > 0){
//print fail message JSON
$successMessage = "fail";
}
//register user
else{
//add user to database
//print json
$d = array('status' => "$successMessage" , 'error' => "$errorMessage" );
$jsonCode = json_encode($d);
header('Content-Type: application/json');
echo $jsonCode;
?>