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.

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

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站