我有一些问题。
首先,我的php文件是shoutbox.php,它用于登录用户。
session_start();
if(! isset($_SESSION["loggedIn"])) {
include "login.html";
}
else {
if(isset($_POST["submit"]) && $_POST["submit"] == "Log In") {
if(! isset($_POST["username"]) || $_POST["username"] == "" || ! isset($_POST["password"]) || $_POST["password"] == "") {
$GLOBALS["logErr"] = "Please, filln all fields!";
echo json_encode(false);
}
else if(isset($_POST["username"]) && $_POST["username"] != "" && isset($_POST["password"]) && $_POST["password"] != "") {
if(isset($_POST["select"]) && $_POST["select"] != "") {
session_start();
$_SESSION["loggedIn"] = TRUE;
$_SESSION["username"] = $_POST["username"];
$_SESSION["password"] = $_POST["password"];
$_SESSION["type"] = $_POST["select"];
$_SESSION["ip"] = get_ip_address(); /* Return current ip */
$user = array(
"username" => $_SESSION["username"],
"islogged" => $_SESSION["loggedIn"],
"password" => $_SESSION["password"],
"ip" => $_SESSION["ip"],
"type" => $_SESSION["type"]
);
echo json_encode($user);
}
}
}
else if(isset($_POST["logout"]) && $_POST["logout"] == "Log Out") {
session_start();
unset($_SESSION["loggedIn"]);
unset($_SESSION["username"]);
unset($_SESSION["password"]);
unset($_SESSION["type"]);
unset($_SESSION["ip"]);
echo json_encode(false);
}
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] == TRUE) {
echo json_encode(true); /* user is logged in */
}
else {
echo json_encode(false); /* user is not logged in */
}
}
返回false或true,如果为true,则返回编码的用户数组。
这是Ajax调用,我必须调用2个响应,false或true,如果响应为true,还是用户数组..
function sb_UserIsLoggedIn() {
$.ajax({
url: 'shoutbox.php',
type: 'POST',
dataType: 'json',
success: function(response, user) {
if(response) {
function user(user) {
CurrUserInfo = {
u_name: user["username"],
u_psw: user["password"],
ip: user["ip"],
typelog: user["type"],
logged: user["islogged"]
};
if(CurrUserInfo.typelog == "anonimous") {
OnlineListObj.onlineList.anonimous.push(user["username"]);
}
else if(CurrUserInfo.typelog == "visible") {
OnlineListObj.onlineList.visible.push(user["username"]);
}
OnlineListObj["onlineList"]["total"] = OnlineListObj.onlineList.anonimous.length + OnlineListObj.onlineList.visible.length;
OnlineListObj["onlineList"]["phrase"] = lang["there_are"] + OnlineListObj.onlineList.total + lang["online"] + OnlineListObj.onlineList.anonimous.length + lang["anonimous"] + OnlineListObj.onlineList.visible + lang["visibles"];
LoggedIn = true;
window.alert(LoggedIn);
window.alert(OnlineListObj["onlineList"]["total"]);
window.alert(OnlineListObj["onlineList"]["phrase"]);
window.alert(CurrUserInfo.ip);
window.alert(CurrUserInfo.typelog);
window.alert(CurrUserInfo.logged);
window.alert(CurrUserInfo.u_name);
window.alert(CurrUserInfo.u_psw);
}
}
else {
window.location.href = "shoutbox.php";
}
},
error: function(response) {
sb_Error("Unknown error, try again");
console.log(lang["ajax_error"]);
console.log(response.responseText);
LoggedIn = false;
},
});
}
警报说这些未定义:
window.alert(CurrUserInfo.ip);
window.alert(CurrUserInfo.typelog);
window.alert(CurrUserInfo.logged);
window.alert(CurrUserInfo.u_name);
window.alert(CurrUserInfo.u_psw);
这是因为对用户数组的调用失败了...但是调用正常,它返回true或false。 为什么它工作而另一个(对用户数组)不?我想ajax调用true或false和用户数组。
其他问题:
在关于sb_isUserLoggedIn()的代码中,我定义了一个var,LoggedIn ......
但是如果你在另一个函数中调用LoggedIn,则返回undefined:
/* Checks if the user is online */
function sb_isOn() {
sb_UserIsLoggedIn();
if(LoggedIn != false) {
return true;
}
else {
return false;
}
}
正如我所写,此代码返回错误,LoggedIn未定义。
如何解决所有这些问题?
提前致谢。
答案 0 :(得分:0)
我认为PHP在成功时会对echo json_encode()
进行两次调用。一个用于回显用户数组,一个用于回显true
。我认为这会导致无效的JSON。
我建议你编写PHP,这样就可以只调用一次echo json_encode()
。你可以让它返回以下成功:
echo json_encode(array("success" => true, "user" => $user));
以下是失败:
echo json_encode(array("success" => false, "message" => 'Please, fill in all fields!'));
然后在Javacript中,您可以拥有以下内容:
success: function(result) {
if (result.success) {
var user = result.user;
// Do something with user here.
} else {
var message = result.message;
// Do something with message here.
}
}