使用Cakephp在Ajax请求上随机获取302

时间:2018-12-20 11:43:40

标签: php ajax cakephp session-cookies

我有一个用cakephp开发的系统。那里有一个客户资料页面,向我显示不同的信息,例如注释,电话,附件。基本信息是通过Cakephp获取的,其他类似上述的信息是通过Ajax请求获取的,并在获取数据后立即显示。 不幸的是,有时并且非常随机(在第10个请求和第100个请求之间),我收到302的Ajax请求,并且将注销并重定向到登录公式(如果您未登录,则为基本重定向路径)。

您可以在图片的Google网页上看到一个示例。图片显示,调用getPhonecalls,getNotes和getOutboxBook很好,但是getFilesBackoffice调用返回302,然后触发登录页面。此后的getAllowedFiles也称为罚款。重要的是要说,它总是一个不同的Ajax调用,因此,我认为任何Ajax调用都可能导致问题。成功的Ajax调用将返回JSON。

enter image description here

我试图重现问题,但对我而言,似乎不可能重现此问题。我还尝试调试该问题,并尝试检查错误可能是什么……但没有机会。 php错误日志也没有提供任何信息。 因此,接下来,我从Chrome下载了完整的“网络信息”,并带有“另存为具有内容的HAR”。查看该文件,好像Cookie(及其Session)被删除了。

以下是状态为302的HAR的摘录:

"response": {
      "status": 302,
      "statusText": "Found",
      "httpVersion": "HTTP/1.1",
      "headers": [
        {
          "name": "Date",
          "value": "Thu, 20 Dec 2018 09:47:27 GMT"
        },
        {
          "name": "Server",
          "value": "Apache/2.4.12 (Unix) OpenSSL/1.0.1p PHP/5.6.11 mod_perl/2.0.8-dev Perl/v5.16.3"
        },
        {
          "name": "X-Powered-By",
          "value": "PHP/5.6.11"
        },
        {
          "name": "Content-Type",
          "value": "text/html; charset=UTF-8"
        },
        {
          "name": "Location",
          "value": "http://localhost/stressfrei/HRMAppMig/Users/login"
        },
        {
          "name": "Connection",
          "value": "Keep-Alive"
        },
        {
          "name": "Set-Cookie",
          "value": "CAKEPHP=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/"
        },
        {
          "name": "Set-Cookie",
          "value": "CAKEPHP=2e5gke00430jdkd8ll5jl3ini3; expires=Fri, 21-Dec-2018 09:47:28 GMT; Max-Age=86400; path=/stressfrei/HRMAppMig/; HttpOnly"
        },
        {
          "name": "Set-Cookie",
          "value": "CAKEPHP=o3udvthe0rkmjt5djrjvnt3392; expires=Fri, 21-Dec-2018 09:47:28 GMT; Max-Age=86400; path=/stressfrei/HRMAppMig/; HttpOnly"
        },
        {
          "name": "Keep-Alive",
          "value": "timeout=5, max=95"
        },
        {
          "name": "Content-Length",
          "value": "0"
        }
      ],
      "cookies": [
        {
          "name": "CAKEPHP",
          "value": "deleted",
          "path": "/",
          "expires": "2018-12-20T09:47:27.127Z",
          "httpOnly": false,
          "secure": false
        },
        {
          "name": "CAKEPHP",
          "value": "2e5gke00430jdkd8ll5jl3ini3",
          "path": "/stressfrei/HRMAppMig/",
          "expires": "2018-12-21T09:47:27.127Z",
          "httpOnly": true,
          "secure": false
        },
        {
          "name": "CAKEPHP",
          "value": "o3udvthe0rkmjt5djrjvnt3392",
          "path": "/stressfrei/HRMAppMig/",
          "expires": "2018-12-21T09:47:27.127Z",
          "httpOnly": true,
          "secure": false
        }
      ],
      "content": {
        "size": 0,
        "mimeType": "text/html",
        "compression": 0
      },
      "redirectURL": "http://localhost/stressfrei/HRMAppMig/Users/login",
      "headersSize": 717,
      "bodySize": 0,
      "_transferSize": 717
    }

以下是200的摘录

"response": {
      "status": 200,
      "statusText": "OK",
      "httpVersion": "HTTP/1.1",
      "headers": [
        {
          "name": "Date",
          "value": "Thu, 20 Dec 2018 09:52:39 GMT"
        },
        {
          "name": "Server",
          "value": "Apache/2.4.12 (Unix) OpenSSL/1.0.1p PHP/5.6.11 mod_perl/2.0.8-dev Perl/v5.16.3"
        },
        {
          "name": "Connection",
          "value": "Keep-Alive"
        },
        {
          "name": "X-Powered-By",
          "value": "PHP/5.6.11"
        },
        {
          "name": "Content-Length",
          "value": "2239"
        },
        {
          "name": "Keep-Alive",
          "value": "timeout=5, max=97"
        },
        {
          "name": "Content-Type",
          "value": "text/html; charset=UTF-8"
        }
      ],
      "cookies": [],
      "content": {
        "size": 2239,
        "mimeType": "text/html",
        "compression": 0,
        "text": "HERE IS SOME JSON"
      },
      "redirectURL": "",
      "headersSize": 287,
      "bodySize": 2239,
      "_transferSize": 2526
    },

这是我的ajax调用示例。

this.ajaxCall = function(offset) {
  var class_prefixTemp = this.class_prefix;
  var self = this;
  $.ajax({
    //ajax options
    type: "POST",
    url: self.ajaxUrl,
    data: {"offset": offset, "elements_per_page": this.elements_per_page, "conditions": this.conditions, "recursive": this.recursive},
    dataType: 'json',

    //handling return
    success: function(result) {
     //print out the data from the result
    }
  });
}

您有什么主意,如何找出问题所在才能最终解决?

1 个答案:

答案 0 :(得分:0)

最后,问题是在core.php中设置了Session。 在CakePhp的core.php中,可以将会话autoRegenrate设置为true。这导致在加载新页面时生成新会话的问题。这可能会导致长时间的Ajax请求使用错误的会话ID,因为它已更新,因此该会话ID不再适合。这导致注销。

最后,我不得不通过将autoRegenerate设置为false来禁用会话更新!