【问题标题】:Compare session variable with post variable via ajax request通过ajax请求将会话变量与post变量进行比较
【发布时间】:2016-12-30 02:47:44
【问题描述】:

我希望当用户验证码完成时发送到页面的请求并将用户输入的字符串与验证码图像的值进行比较(此值存储在会话值中)

这是包含 ajax 请求的页面(这里不是 img 标签,但它并不重要!):

<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input id="captcha" maxlength="5" onkeyup="myFunction()" value="">
<script>
function myFunction() {
    var x = document.getElementById("captcha"); 
    if(x.value.length==5){
      $(document).ready(function(){
        $.get("test.php",
          function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
          });
        });
    }
}
</script>
</body>
</html>

这个页面就是获取发布请求:

<?php
session_start();
$c=$_POST['captcha']; echo $c;echo gettype($c);
$s=$_SESSION['captcha']; echo $s;echo gettype($s);
if($c==$s) {
    echo 'correct';
} else {
    echo 'incorrect';
}
?>

但作为回应:会话变量的类型是NULL!为什么?!

但是 post 变量的类型是字符串,但我确定 session 和 post 是相等的。

现在怎么比较呢?

【问题讨论】:

  • 您需要更新名为“myFunction”的函数,并需要遵循实际的 ajax 请求系统...

标签: ajax session post


【解决方案1】:

这个问题解决了!但我不知道到底如何!

我从事 serach ajax 工作,当然还会再次开始会话。

但在工作前后都没有问题。

但我认为更好的是在发送请求的页面上,会话开始。

它成功了!

然后我删除了 session_start() 语句,但它仍然有效:D

我认为代码没有心情 :))

页面发送请求代码:

<html dir="rtl">
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<img id="captcha" src="captcha.php" width="160" height="45" border="1" alt="CAPTCHA">
<small><a href="#" onclick="
document.getElementById('captcha').src = 'captcha.php?' + Math.random();
document.getElementById('captcha_code').value = '';
return false;
">refresh</a></small>
<input id="fname" type="text" name="captcha" size="6" maxlength="5" onkeyup="myFunction()" value="">
<small id="massage-captcha">input number show on the image.</small>
<script>
function myFunction() {
var x = document.getElementById("fname"); 
if(x.value.length==5){
    document.getElementById("massage-captcha").innerHTML = "please wait!";
    $(document).ready(function(){
        $.post("test.php",
        { 
        captcha: x.value
        },
        function(data,status){
            if(data=="correct"){
                document.getElementById("massage-captcha").innerHTML = "correct";}else{
                document.getElementById("massage-captcha").innerHTML = "incorrect";
            }
        });
    });
}
else{
    b.value="";
}

}
</script>

和文件接收请求:

<?php
session_start();
if($_POST['captcha']==$_SESSION['captcha']){echo 'correct';}
else{echo 'incorrect';}
?>

我的最后一个想法:在过去 pne 天后删除了服务器上的会话,现在可以正常工作了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2016-08-26
    • 2018-05-01
    • 1970-01-01
    • 2014-01-30
    • 2010-12-24
    • 2020-10-20
    相关资源
    最近更新 更多