Jump to content

$session variable disappeares when Ajax loaded ?


Recommended Posts


I'm facing a weird issue here. I have a page loaded with this code inside (my comments in line ends) :

if ($session->allPlayers) { // Set in a head.inc file. I have also a $session->set('allTeams', $allTeams); in my head.inc
  $allPlayers = $session->allPlayers;
} else {
  $allPlayers = getAllPlayers($user, false);
  $session->set('allPlayers', $allPlayers);
bd($session->getAll()); // HERE, I get a number of 11 variables which is what I expect

In the same page, I have a link pointing to ajaxContent.php that loads stuff via Ajax.

I just write this in my ajaxContent.php to test :

bd($session->getAll()); // HERE, I get only 9 variables. All my newly set $session variables ($allTeams and $allPlayers) are not conveyed to ajaxContent.php ???

Would you have any idea why is that ??? Another thing : I have a $session->headMenu set in my head.inc, and this one works fine. I can retrieve it in my ajaxContent.php page.

I've tried cleaning all caches but it doesn't change anything 😞 

At first, I expected it to be a 15-minute update to my site... It turns out to be a 2-hour issue and I'm still  stuck.

Thanks for your ideas ! 

Link to comment
Share on other sites

What does getAllPlayers() do? Does this contain a selector with permission access? Perhaps checking in a incognito screen to check if you get both scenarios the same variables.

Link to comment
Share on other sites

Thanks for your concern @arjen. I appreciate your help.

35 minutes ago, arjen said:

What does getAllPlayers() do? Does this contain a selector with permission access?

It doesn't do much except a $pages->find("template=player, team=$selectedTeam") according to the logged in user.

But your comment made me think of another test. I simplified by wroting :

 $session->set('allPlayers', 'test');

But I get nothing on my ajaxContent.php page... $session->getAll() lists previously set variables only.

So I kept trying things and went on delogging my user and trying to re-log and noticed it didn't work as expected any longer. So I switched to my dev branch and logging in functionality came back. So I started inspecting my code and here's what I noticed :

  • If I put this in my head.inc :

$session->set('allTeams', 'test');

Everything works as expected. No more issue anywhere.

  • If i put this instead :

$session->set('allTeams, $allTeams);

$allTeams being a pageArray that the actual logged-in user receives from a $pages request (actually the different classes for the logged-in teacher), my logging-in system doesn't work any longer and I get my initial issue. Actually, I interpret this as an impossibility to update in any way my $session...


So I'm slowly stepping forward, but I still don't understand anything about this behavior...

You also mentionned :

52 minutes ago, arjen said:

Perhaps checking in a incognito screen to check if you get both scenarios the same variables.

And I have no idea of what that means, sorry... If incognito screen is a 'guest' view, for the moment, I just want a specific role to have this functionality.

I stay tuned for explanations 🙂 

Link to comment
Share on other sites

Oh, I've just re-re-re-re-read the API documentation, and maybe I've seen something :

Does $session->set($key, $value) only work with strings for $value ? Maybe this is my issue trying to pass a PageArray ???

And then, how would you do that, then, passing a 'big' pageArray without having to do the DB request again ? I thought I could do it through $session ?

Link to comment
Share on other sites

You are safe to store a regular array in the session variable. See this old thread: 

I would worry to much about performance since only looking up ID's is very fast since you are not joining.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Liam88
      I'm really struggling with this as it's something not in my wheelhouse. I'm creating a blog style page (a grid of cards) which has attributes.
      I have a snip of javascript which grabs values from checkboxes which are put into a value like the below:
      document.querySelector("form").onsubmit=ev=>{ ev.preventDefault(); let o={}; ev.target.querySelectorAll("[name]:checked").forEach(el=>{ (o[el.name]=o[el.name]||[]).push(el.value)}) console.log(location.pathname+"?"+ Object.entries(o).map(([v,f])=> v+"="+f.join("_")).join("&") ); document.location.href = location.pathname+"?"+ Object.entries(o).map(([v,f])=> v+"="+f.join("_")).join("&"); } As I'm currently refeshing the page on button click with those values the end result includes the location but can easily remove this.
      I then use this value in "input->get" to get the values which I then append to a find() rule. See code below:
      $selector = "template='adbank_pages',sort=published,include=all,status!=hidden"; // Get the channel and content inputs $channel = $input->get->channel; $content = $input->get->content; if($channel){ // Grab the channel string, explode into an array for checkbox checking and then replace _ with | to create or rules in the selector. $chanArray = explode("_", $channel); $chan = $channel = str_replace('_', '|', $channel); $selector = $selector .= ",ab_channels=$chan"; } if($content){ // Grab the content string, explode into an array for checkbox checking and then replace _ with | to create or rules in the selector. $contArray = explode("_", $content); $cont = $content = str_replace('_', '|', $content); $selector = $selector .= ",ab_content=$cont"; } if($input->get){ // If a valid input result $all = $pages->find($selector); } }else{ // If no input show them all $all = $page->children("template='adbank_pages',sort=-published,include=all,status!=hidden"); } $items = $all->find("limit=12"); // Limit the output and use pagination As mentioned above I currently refresh the page to adjust the $selector filter within the $all with a fallback $all if there are no results.
      I know I need to use AJAX to filter the content without refresh but I am really struggling with the set up. I have read multiple posts including the original by Ryan but still confused.
      If anyone can direct/help on this it would be appreciated.
      Thank you
    • By theoretic
      Hi guys and ladies! And thanks for Processwire!
      It appears i've got an interesting issue concerning the template-settings-based PW redirects dealing with access control. Any PW template has some access control options i.e. "Login redirect URL or page ID to render". If this option is used for a page having a template with this option filled, a redirect will occur if user is not logged in and/or has insufficient access rights.
      I like to hook PW events. In one of my current projects i decided to write an addHookBefore('Session::redirect', ...) which should store the page we are being redirected from. With "regular" redirects like $session->redirect('/somewhere/') this hook works like a charm. But it was strange to see that it doesn't work with the template-settings-based redirect.
      I'm too dumb to dive deep inside PW and to examine the whole PW session mechanism. But it could be rather logical if ANY redirect ( no matter template-settings-based or using $session->redirect() ) could be hooked in the same manner.
      Okay okay i can forget about template-settings-based redirect and write my own. Just a couple of lines of code, and it works. But it's less elegant than hooking the template-settings-based redirects.
      So am i missing something? It this behavior a bug, or is it intended by PW team? Thanks in advance for any comment!
    • By fliwire
      Hi, after redirect to payment page processwire session lost because of samesite cookies changed default to "lax".


      tried to hook session::init but not works ?
      $wire->addHookBefore("Session::init", function (HookEvent $event) { ini_set('session.cookie_samesite', 'None'); session_set_cookie_params(['samesite' => 'None']); });

      set by htaccess works
      <ifmodule mod_headers.c> Header always edit Set-Cookie ^(.*)$ $1;SameSite=None;Secure </ifmodule>  
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      //edit: I've noticed something interesting; despite the directories of my web folders being the same layout; when I open the admin page i get a 404 on the processwire/ resource in the networks panel of chrome; on my laptop I get a  200.... I guess this is where my problem is; but why?
  • Create New...