dongqian5639 2015-06-03 16:46
浏览 83

ajax调用期间的cookie与进行ajax调用的页面上的cookie不同

First of all, this is all happening on the same base domain, and I'm not making any ajax calls off site to a different URL.

I'm making a simple ajax call every 10 seconds from any page on my site to a php class, and based on the user information in the cookies which is assigned when the user is logged in, it will update a last_active datetime value to the current timestamp in the database for that user. This is mainly used for displaying on a page I made of users who currently have the site open on their screen.

I noticed that if I log out on one user, then log in as a different user, the cookie updates fine after logging in. However, it seems when I make the same ajax call to update the last_active datetime, it is updating the previously logged in user's time instead.

I found that during the ajax call and only during the ajax call, the cookies still contained the previous logged in user's information.

I've never had this problem before, so I'm unsure what is causing it or how to fix it without requiring another ajax call to manually set the cookie to the right user.

As for sample code, I guess this is the best way I can give an example.

On my index.php, I included a script index.js. In the script, I have it making an ajax call every 10 seconds to a php script in the app folder.

$.ajax({
    type: "POST",
    url: "app/ajax.php",
    data: {
        'class': 'App_User',
        'function': 'updateLastActive',
        'user_account_id': user_account_id
    },
    dataType: "json",
    success: function(data) {
    }
});

The above code will make a call to the app/ajax.php with the given data values. Basically in ajax.php, it will initiate the App_User class then run the updateLastActive function with the user_account_id as an argument.

As I mentioned, the whole ajax calling process works fine. The only problem is, for example, if I do a var_dump($_COOKIE) from within the App_User class during the ajax call, versus if I did a var_dump($_COOKIE) from index.php before or after the ajax call, the information is different.

From the front end perspective, here is what I see when tracing the cookie info.

  1. I log into the site as user A, whose user_account_id is 12
  2. The var_dump from index.php shows user_account_id as 12, which is correct.
  3. Ajax call is made, the var_dump from within the App_User class during the call shows user_account_id as 12, which is correct.
  4. I log out, then log back into the site as user B, whose user_account_id is 10
  5. The var_dump from index.php shows user_account_id as 10, which is correct
  6. Ajax call is made, the var_dump from within the App_User class during the call shows user_account_id as 12.
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)