dtz88967 2015-09-08 00:45
浏览 71
已采纳

jquery发布ajax调用会话不起作用

I'm really lost here while trying to send a session with my jquery ajax post call, here is a simplified example of my code.

File fom which request is sent:

<?php
    session_start();
    $token = md5(rand(1000,9999));
    $_SESSION['contactToken'] = $token; 
?>

<script type="text/javascript">
    $.post(ContactUrl,{req:"contact_sub",tok:"<?php echo $token; ?>"},function(contactAns){
        alert(contactAns); return false;
    });
</script>

File request is sent to:

<?php
if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']=="url"){
    if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) ){

        session_start();
        $token = $_POST['tok'];
        $sess_token = $_SESSION['contactToken'];

        if($token == $sess_token){
            echo "sessions match"; exit();
        }
        else{
            echo "sessions does not match"; exit();
        }
    }
    else{echo "error"; exit();}
}
else{echo "error"; exit();}
?>

At first the session was not getting recognized, I made all the checks - made sure it was setup in the first place made sure it was posted, declared session start on both pages, never the less if i tried to do this on the second file:

<?php
session_start();
$token = $_POST['tok'];
$sess_token = $_SESSION['contactToken'];

print_r($_SESSION['contactToken']); exit();
?>

I would get an empty alert. Then I transferred the session start to the top of my script on the second page and started getting a value for the session:

<?php
session_start();
$sess_token = $_SESSION['contactToken'];

if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']=="url"){
    if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) ){

        $token = $_POST['tok'];
        echo "$token, $sess_token"; exit();

    }
    else{echo "error"; exit();}
}
else{echo "error"; exit();}
?>

And what I'm getting now is that the posted variable changes each time I refresh the page but the $sess_token always gives me the same value: 0589dd536fd043ff3865f8223fef3030

I really dont understand this wierd behavior, can some one please assist me with this?

  • 写回答

1条回答 默认 最新

  • dongye9182 2015-09-08 01:31
    关注

    Your problem here is that you're using a PHP var in an JS script without wraping and echoing it.. Here is your code modified:

    You're also trying to contatenate with . in JS. That's from PHP too.

    <script type="text/javascript">
        $.post(ContactUrl, {
            req: "contact_sub",
            tok: "<?php echo $token; ?>"
        }, function(contactAns) {
            alert(contactAns); 
            return false;
        });
    </script>
    

    Update

    I came back to this answer again today. This is what I did:

    FILE: index.php

    <?php
        session_start();
        $token = md5(rand(1000,9999));
        $_SESSION["contactToken"] = $token; 
    ?>
    
    <script type="text/javascript">
        $.post("myOtherScript.php", {
          req:"contact_sub",
          tok:"<?php echo $token; ?>"
        }, function(contactAns){
          alert(contactAns);
          return false;
        });
    </script>
    

    FILE: myOtherScript.php

    <?php
       session_start();
       $sess_token = $_SESSION["contactToken"];
    
       if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && ($_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest")){
    
          $token = $_POST["tok"];
          echo $token ." - ". $sess_token;
    
       } else {
          echo "Not an AJAX request";
       }
    ?>
    

    What I get is the alert where one token is equal to the other and both are refreshed each time I reload the index.php file.

    As a conclusion, your problem is not in the code you shared.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序