Basit Munir Basit Munir - 1 year ago 55
PHP Question

PHP cookie is being auto delete or not being set on frequent AJAX request

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


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,'/', '');

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(){
type: "POST",
url: SiteVars.siteUrl+"Logout.php?sess=idle",

success: function(data)




Answer Source

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.


    $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));