doulizhi1247 2014-08-15 16:33
浏览 77
已采纳

会话不适用于ajaxRequest [如何使用会话变量计算ajax-request]

I have the following PHP code which counts every access to the page. If I call the side direct it counts certain. If I make a Ajax call it starts always again with 1.

<?php
session_start();    
header('Access-Control-Allow-Origin: *');

        if (!isset($_SESSION['zaehler'])) {
          $_SESSION['zaehler'] = 1;
        } else {
          $_SESSION['zaehler']++;
        }
echo  $_SESSION['zaehler'];


?>

I understand that an ajax call is the same like an direct call by reading this: Do AJAX requests retain PHP Session info? How can I count each ajaxCall as long as the side is open?

I'm doing a simple jquery ajaxCall:

    $.get( "http://www.huntinggrounds.de/aa.php", function( data ) {
         console.log( "dataResponse: ", data );
    }) .fail(function(jqXHR, textStatus ) {
        console.log( "error",textStatus);
       })
       .always(function() {
        console.log( "finished" );
       });

I made test-files, testfile.html, just a black screen. Click on desktop and see the counter in your console the php-file is here.

  • 写回答

3条回答 默认 最新

  • dongzhang1987 2014-08-18 12:01
    关注

    After testing your pages, your problem is related to your domain.

    http://www.huntinggrounds.de/aa.html is working fine, not http://huntinggrounds.de/aa.html

    For security reasons, cookie is per-domain specific. Cookie set by PHP on http://huntinggrounds.de/aa.html will not work on http://www.huntinggrounds.de/aa.html depending of your configuration.

    To by pass this problem, don't set full URL in your ajax. Preferer relative URI.

    $.get( "/collect?"+setParameter(defs),
     function( data ) {
          try {
                 console.log("New count is : ",data);
              } catch(e) {
                 // if there is no response
                 console.log( "No answer : ", data);
             }
      }) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥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系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。