【问题标题】:json_encode boolean returning wrong valuejson_encode boolean 返回错误值
【发布时间】:2012-07-24 20:56:26
【问题描述】:

我正在编写一个 jquery 登录。如果 php 登录脚本 (checklogin.php) 回显“true”,那么我的 jquery 页面应该会发出警报。当我运行 login.php(不使用 jquery)时,它会将请求发送到 checklogin.php,它会回显“true”但是如果我使用我的 jquery 登录脚本将登录信息发送到 checklogin.php,我跟踪的警报后面说“假”。

$('#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);
}
...
?>

【问题讨论】:

  • json_encode 是在某处实际使用的,还是只是在收集那个 echo "true";作为 ajax 响应?
  • 请看我的编辑。这是我最初尝试的,但无论我做什么它都会返回 null 所以我认为尝试一个简单的字符串或布尔检查会更容易,一旦我开始工作,我会再试一次。
  • @RapsFan1981:查看更新的答案。

标签: php jquery json login


【解决方案1】:

使用true,而不是'true''true' 是一个字符串,你想要一个布尔值:

$json = array('boolean' => true, 'jsonUser' => $myusername);

另外,永远不要== true

if(data<del>==true</del>) {

好的,现在问题是:

$json = array('boolean' => true, 'jsonUser' => $myusername);
echo true;

不要echo trueecho json_encode($json)

【讨论】:

  • “从不写 == true” -> 为什么?
  • @Mahn:它只会导致代码错误。 if(data == true)通常等同于if(data)。如果等价,== true是多余的,应该去掉。如果它 not 等价,则您的代码存在严重问题 - 就像这里的情况一样。如果您确实需要检查 true 值,最多使用 === true
  • 嗯,可能只有我一个人,但我很难想象在什么情况下if(data == true) 不等于if(data),你能举个例子吗?只是好奇。
  • 无效。警报(“假”);仍在射击。
  • @Mahn:好吧,JavaScript。 'true' != true'true' 是真实的。
猜你喜欢
  • 2013-10-24
  • 2013-10-26
  • 2010-12-30
  • 1970-01-01
  • 2016-03-03
  • 2019-02-03
  • 1970-01-01
相关资源
最近更新 更多