我正在爲我的電子郵件表單構建一個簡單的抗體。 這使得問題的代碼:使用PHP和JQuery AJAX的Antibot
<?php
session_start();
$a = rand(1, 10);
$b = rand(1, 10);
$antibot = $a + $b;
$_SESSION["antibot"] = $antibot;
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#sendEmail").click(function(){
var antibot = $("#antibot").val();
$.post(
"test.php",
{antibot: antibot},
function(data){
alert(data.info);
},
"json"
);
);
);
);
</script>
</head>
<body>
<table>
<tr><td>AntiBot: <?php echo $a . ' + ' . $b;?> = </td><td><input type='text' id='antibot' /></td></tr>
<tr><td colspan='2'><input type='button' id='sendEmail' value='Send'/></td></tr>
</table>
</body>
</html>
我的test.php的
<?php
session_start();
$antibot = $_POST["antibot"];
$data = array();
if(isset($_SESSION["antibot"])){
if($_SESSION["antibot"] == $antibot){
$data["info"] = "Session is isset and answer is right!";
}
else{
$data["info"] = "Session is isset but answer is NOT right!";
}
}
else{
$data["info"] = "Session is NOT isset!";
}
echo json_encode($data);
?>
我不斷地得到這個信息:「會議是isset,但答案是不正確的」 如果你可以看到test.php中的$ _SESSION [「antibot」]被設置,但是無論我在輸入字段#antibot
中輸入了什麼,值都是""
!
我不明白爲什麼會發生這種情況,請有人告訴我問題在哪裏,我該如何解決?
你不應該檢查isset($ antibot)嗎? – MimiEAM 2012-08-16 05:51:18
沒關係。我查了好多次,唯一的問題是$ _SESSION [「antibot」]有一個空白值。 – consigliere 2012-08-16 05:53:00
我已經通過您的代碼,這是我首先理解您生成兩個隨機數字並總結它並將它保存爲會話中的抗體。然後在表單中,用戶在抗體字段中輸入一些值。當用戶提交的頁面值被髮送到test.php並且在test.php中,您正在匹配會話值和用戶輸入的抗體值。那是對的嗎 ? – Nick 2012-08-16 05:58:51