PHP cookie正在自动删除或未在频繁的AJAX请求上设置

PHP cookie正在自动删除或未在频繁的AJAX请求上设置

问题描述:

I got a weird situation from yesterday, and it is really embarrassing that cookie is not being set.

Scenario:

1) system have AJAX call to check user session if it is alive or not while user is not doing any activity on page.

2) Request sent to server side every minute to check user session if user is idle for specific minutes it send request to expire session to logout page.

3) On logout page it expire session because of inactivity and set cookie for record, when user re-enter credentials it will refresh session where it left.

Problem is. Cookie not being set while I had tried following scenarios. using all parameters

setcookie("config_transactionID", $_SESSION['cableTransactionID'], $expire,'/', 'http://domain.com');

used root path

setcookie("config_transactionID", $sessionValue, $expire,'/');

used basic paramters.

setcookie("config_transactionID", $_SESSION['cableTransactionID'], $expire);

Tried header_remove too, to remove all headers before setting cookie.

used some static content to set cookie if it is being set or not. but system returns

 set_cookie returns: bool(true)

Code which send request to back end

function expireSession(){
$.ajax({
    type: "POST",
    url: SiteVars.siteUrl+"Logout.php?sess=idle",
    data:{test:"test"},

    success: function(data)
    {

    }//

 });//

}

我从昨天开始就遇到了一个奇怪的情况,而且没有设置cookie真的很尴尬。 p>

场景: p>

1)如果用户未在页面上进行任何活动,系统会进行AJAX调用以检查用户会话是否存在。 p>

2)每分钟发送到服务器端以检查用户会话的请求 如果用户空闲了特定的分钟数,它将请求发送到注销页面的会话到期。 p>

3 )在注销页面上,由于不活动而设置会话到期,并设置cookie用于记录,当用户重新输入凭据时,它将刷新它离开的会话。 p>

问题是。 Cookie未设置 我曾尝试过以下方案。 使用所有参数 p>

  setcookie(“config_transactionID”,$ _SESSION ['cableTransactionID'],$ expire,'/','http://  domain.com'); 
  code>  pre> 
 
 

使用的根路径 p>

  setcookie(“config_transactionID”,$ sessionValue,$  expire,'/'); 
  code>  pre> 
 
 

使用的基本pa ramters。 p>

  setcookie(“config_transactionID”,$ _SESSION ['cableTransactionID'],$ expire); 
  code>  pre> 
 
 

尝试header_remove,在设置cookie之前删除所有标题。 p>

如果正在设置cookie,则使用一些静态内容来设置cookie。 但系统返回 p>

  set_cookie返回:bool(true  )
  code>  pre> 
 
 

向后端发送请求的代码 p>

  function expireSession(){
 $ .ajax(  {
 type:“POST”,
 url:SiteVars.siteUrl +“Logout.php?sess = idle”,
 data:{test:“test”},
 
成功:函数(数据)
  {
 
} // 
 
}); // 
 
} 
  code>  pre> 
  div>

Shah Rukh suggestion worked for me. problem with the scrip was frequent session expiry requests. Repetitive session expiry requests were resetting cookie value with NULL (while session was expired after first one).

I added a check where I was setting a cookie and additionally added random string to actual value to make sure it will never be empty.

if(!isset($_COOKIE['config_transactionID'])){

    $expire = time() + (60 * 60 * 24 * 30);
    $transaction = base64_encode($_SESSION['cableTransactionID'].'--'.md5(rand(999,9999)));
    $var = setcookie("config_transactionID", $transaction, time()+ (60*60*24*30));

}