我有一个正在实现的登录系统,当我使用从 jQuery $.post 调用的 PHP 脚本时,它会添加 \r\n、一些空格,然后再次添加 \r\n(如使用 firebug 所见)。
我正在尝试返回“成功”一词(如果登录有问题,则返回其他词),以便我可以确定登录请求的状态。在比较之前,我总是可以去掉斜线并修剪输出,但如果我能解决这个原因,我宁愿不必这样做。
这个 PHP 的输出是 "Success\r\n \r\n" 而不仅仅是 "Success" :
<?php
// session_destroy();
$lname = $_POST['lname'];
$passhash = $_POST['pass'];
include 'includes/dbiconnect.php';
$loginStatus = "NA";
$query = "SELECT `txt_LOGINNAME`, `txt_PASSHASH`, `txt_PILOTNAME`, `bool_SYSADMIN` FROM `tbl_staff_details` WHERE `txt_LOGINNAME` = '{$lname}' AND `bool_CURRENT` = 1";
$result = mysqli_query($db_link,$query) or die('cannot open DB for login!');
if (is_null($result)) {
$result_row_num = 0;
} else {
$result_row_num = mysqli_num_rows($result);
$loginDetail = mysqli_fetch_assoc($result);
}
switch (true) {
case ($result_row_num == 0):
$loginStatus = "Not Found";
break;
case ($result_row_num > 1):
$loginStatus = "Too Many";
break;
case ($loginDetail['txt_LOGINNAME'] == $lname):
// The Default:
$loginStatus = "Incorrect Password";
// Deviate from default only if PW Hashes match
if ($passhash == $loginDetail['txt_PASSHASH']) {
session_start();
$_SESSION['FullName'] = $loginDetail['txt_PILOTNAME'];
$_SESSION['SysAdmin'] = $loginDetail['bool_SYSADMIN'];
$_SESSION['LoginName'] = $loginDetail['txt_LOGINNAME'];
$_SESSION['lastActivity'] = time();
$loginStatus = "Success";
} // if
} // switch
mysqli_close($db_link);
print "{$loginStatus}";
?>
处理此问题的 Javascript 是(包括一些帮助我可视化输入/输出的警报语句):
case (passwordentered != "" && usernameentered != ""):
$("#errorField").val("I can work with this");
var passwordhash = CryptoJS.SHA256(passwordentered);
var str_hash = passwordhash.toString(CryptoJS.enc.Hex);
alert(passwordhash);
$.post( 'validatelogin.php' , {lname : usernameentered, pass : str_hash},
function(output) {
alert(output);
switch (output) {
case "Success":
$("#errorField").val("Login Was Successful");
window.location("./daysheets.php");
break;
case "Incorrect Password":
$("#errorField").val("ERROR: Incorrect Password");
break;
case "Not Found":
$("#errorField").val("ERROR: User Not Found");
break;
case "Too Many":
$("#errorField").val("ERROR: Not Unique");
default:
var loginerror = output + ", Login Err";
$("#errorField").val(loginerror);
}
});
break;
如果有人可以告诉我在 jQuery 获取它时将附加字符附加到输出的位置/原因,将不胜感激。
问候
布雷登