Jump to content
bramwolf

Not working $session variables

Recommended Posts

Hey Guys,

I've ran into some really weird behavior with session variables.

What I've been trying to do is this:

I have 10 separate pages that share the same template, named cursus.php.

These are pages with course info and have different title's. these page all

do the following:

$session->remove($cursus);

$session->set($cursus, $page->title);

They all have a link to one page where you can subscribe for the course

on that page I call out the session variable and want to recall there

from what page they entered the subscription page by doing the following:

 <div>Subscribe for the course: <?PHP echo $session->get($cursus); ?></div> 

and that, on it's turn, gets mailed to my client.

Now Firefox and Safari seem to be displaying the variable perfectly fine but Chrome 

is not. I've tried it on my mobile and and desktop and even in incognito mode, which

should clear any cache problems, but it still doesn't work. 

Am i going about this completely the wrong way or does anyone have a suggestion of

what the problem could be? As you can image it's very important to my client and thus

to me, that I get this working as soon as possible!

Any help will be greatly appreciated.

Bram Wolf

Share this post


Link to post
Share on other sites

Do you really use:

$session->set($cursus, $page->title);

rather than:

$session->set('cursus', $page->title);

or was that a typo in the post?

Share this post


Link to post
Share on other sites

Or just do

$session->cursus = $page->title;

and

$session->cursus = NULL;

and to retrieve

echo $session->cursus;

Share this post


Link to post
Share on other sites

Yeah I did,

Copied it from the cheatsheet?

http://cheatsheet.processwire.com/session/properties-and-methods/session-set-name-value/

I do have the feeling it works? on some browsers? I have no idea what, and why it isn't working properly :P

Well unless you have previously defined $cursus to a value you should end up getting this error:

Notice: Undefined variable: cursus

Although even with that error, it does seem to work ok. I agree with @junofornow - that syntax is much cleaner, although I would use:

$session->remove('cursus');

Sorry, I am not sure why you are having issues with Chrome - I might be completely off base here, but are either of these any help?

http://stackoverflow.com/questions/8247842/session-data-lost-in-chrome-only

http://stackoverflow.com/questions/2953536/randomly-losing-session-variables-only-in-google-chrome-url-rewriting

Share this post


Link to post
Share on other sites

Hey Guys,

Thanks for the replies! I tried your different syntaxes however with those Firefox, Safari and Chrome failed to display the variable.

I'm not sure how I would define a session variable, other than I did? 

Further I did some testing and read somewhere cookies solved the problem, and cookies also work fine on Firefox and Safari.

But Chrome still doesn't, It sets them properly and gives them the session expiration date but when I go to a different page,

in this case the subscription page, it gets removed from the cookie list. A think Chrome might be doing the same to Processwire's

session variable? I don't know where those are stored but they don't show up in the cookie list.

Has anybody heard of this problem? I set cookies like this:

document.cookie = "cursus=<?PHP echo $page->title ?>; path=/";
 
 
and this to retrieve it:
 
<script type="text/javascript">
function getCookie(name)
  {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
  }
</script>
 
<script type="text/javascript">      
document.write(getCookie("cursus"));
</script></div>
 
 
Oh btw, When searching Google I keep on finding issues where Chrome doesn't clear the session cookies.. :S
Well at least I fixed that for everyone, my Chrome super-clears cookies :P
 
 

Thanx in advance :)

Share this post


Link to post
Share on other sites

YES!

I fixed it :) On the course-page, where I set the cookie, and the session variable in the first case, I linked to the subscription 

page where in turn I wanted to call the cookie. I figured out that on pretty much all the pages within the site Chrome

kept the cookie alive, except for the subscription page. I saw al links inside my site ended with a slash, checked

the code and saw that the link I made didn't end with the slash.

Also, very weird but I didn't add a domain to the cookie creation, and the course page created a www.domain.com instance

while the subscribe page seemed to be looking for a .domain.com instance, adding a domain to the cookie fixed this as well.

I added it and now chrome keeps the cookie alive :)

Pretty weird that this make so much difference, and that Firefox and Safari don't take the trailing slash into account 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By celfred
      Hello,
      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 ! 
    • By Peter Knight
      I have a demo site which I moved to a new VPS for client testing
      We noticed that leaving a page open and then revisiting the site can result in a 25 second(ish) to load time and will then throw a 500 Error.
      The hosting guys had a look and confirmed that the server is fine but the issue could be related to authentication or sessions.
      We are running Page Protector and ProCache so I wondered if there were any known bugs here and any recommended actions.
      My actual PW log doesn't show anything but the server log has plenty of these
       
      2018-12-06 08:14:00 Error xxx.141.1x.101 500 POST /who-we-are/ HTTP/1.0     1.58 K Apache access 2018-12-06 08:14:45 Warning xxx.141.1x.131   mod_fcgid: read data timeout in 45 seconds, referer: http://demo.abc.not/who-we-are/       Apache error 2018-12-06 08:14:45 Error xxx.141.1x.131   End of script output before headers: index.php, referer: http://demo.abc.not/who-we-are/       Apache error 2018-12-06 09:03:18 Error xxx.141.1x.131   2614#0: *667 recv() failed (104: Connection reset by peer) while reading response header from upstream       nginx error Thanks
      P
    • By FrancisChung
      Long but well written, detailed and informative article written by an Engineering Manager for Google Chrome about the true cost of Javascript and what you can do to alleviate some of that cost.
      Must read!

      https://medium.com/@addyosmani/the-cost-of-javascript-in-2018-7d8950fbb5d4
    • By alejandro
      Hello, after creating an array and assigning it to a variable in session:
      $order = array (); $order['token'] = 'token'; $order['product'] = 'product-name'; $session->order = $order; I try to insert another item:
      $session->order['price'] = 'price'; But it doesn't work: 
      Notice: Indirect modification of overloaded property ProcessWire\Session::$order has no effect in... Isn't possible to modify such session variable? I could build another array from the session variable, add the new item, and then store it again in session, but doesn't looks good.
      Thanks!
    • By dragan
      If I have two PW sites that sit in separate folders, I can't be logged-in in both sites.
      e.g.
      site.com/project-a/pw-admin-slug/
      site.com/project-b/pw-admin-slug/
      If I login to project-a, then also login to project-b, get back to the first site, I have to login again.
      Is the cookie / session mechanism storing my domain? If it does, and it's meant to be some sort of security enhancement, it should not check my domain, but root-URL of the PW-installation. (strangely, this doesn't happen on localhost)
      Is it possible to prevent that behavior? Often I have two sites open (e.g. check to see if I have the same CKEditor setup and quickly copy and paste it, or copy a user-role)
×
×
  • Create New...