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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置