Redis Cluster for php session and ajax call not save session

  Uncategorized

It was been a couple of days I’ve got an issue with php session (in cakePhp 4 project) with a redis cluster and ajax call that not save in session.

Use case :
I’m on a page with a session, session works perfectly. on this page there is à button that call an ajax action. On https://www.mywebsite.com ajax call https://www.mywebsite.com/ajaxAction (so no cross origin pb like I see a lot of time)

ajaxAction is very simple :

public function ajaxAction()
{
    $this->disableAutoRender();
    $treeId = (int) $this->request->getData('treeId');
    var_dump($_SESSION['myArray']); // empty
    $_SESSION['myArray'][$treeId] = true;
    var_dump($_SESSION['myArray']); // array with the value
}

If I click to add an other $treeId in the session, the first debug is empty, and the second has only the new value.

When I check the session value in redis-cli, the value is not in the session.

If I put a sleep(1) after the $_SESSION[‘myArray’][$treeId] = true; the value is save in the session.

If I’m trying the same type of call with two simple php script (without cakePhp), it works perfectly….

Someone has a solution to understand the pb to find a solution ?
Is it from cakePhp ? Redis ? Ajax ?

Thanks for you’re help

Source: CakePHP

LEAVE A COMMENT