我正在编写一个jQuery登录页面。如果PHP登录脚本(checklogin.php)回显“true”,那么我的jQuery页面应该弹出警报。当我运行不使用jQuery的login.php时,它会将请求发送到checklogin.php,后者回显“true”。但是,如果我使用我的jQuery登录脚本将登录信息发送到checklogin.php,则回溯的警报会显示“false”。
$('#login').click(function(e){
$.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data==true){
alert(data.boolean);
//$('#logindiv').slideUp();
//$('#gamediv').show();
//$('#registerdiv').hide();
//$('#gameheader').html(data['username']);
}else{
alert("false");
}
});
});
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="hedonsof_conflict"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
$json = array('boolean' => 'true', 'jsonUser' => $myusername);
echo "true";
}
else {
echo "Wrong Username or Password";
}
?>
简单地说,当我尝试从常规的php页面登录时,我追踪到的值为true;当我尝试从jquery页面登录时,我追踪到的值为false。
编辑:
<?php
...
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
$json = array('boolean' => true, 'jsonUser' => $myusername);
echo true;
}
else {
echo "Wrong Username or Password";
}
?>
我使用 jQuery ...
$('#login').click(function(e){
$.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data){
alert("true");
//$('#logindiv').slideUp();
//$('#gamediv').show();
//$('#registerdiv').hide();
//$('#gameheader').html(data['username']);
}else{
alert(data); // shows null
}
});
});
使用
json_encode
时,布尔值会返回null
。$('#login').click(function(e){
$.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data.boolean == "true"){
alert("true");
//$('#logindiv').slideUp();
//$('#gamediv').show();
//$('#registerdiv').hide();
//$('#gameheader').html(data['username']);
}else{ alert(data);
}
}); });
<?php
...
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
$json = array('boolean' => true, 'jsonUser' => $myusername);
echo json_encode($json);
}
...
?>