Jump to content

Not working $session variables


bramwolf
 Share

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

Link to comment
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

Link to comment
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 :)

Link to comment
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
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
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By wwwouter
      Some context: I want to use PHP variables in my CSS (more info below) and found a solution on CSS-tricks that looks fairly elegant and somewhat solid to me. It's pretty simple, I created a file style.css.php inside the site/templates/ directory and load that in my page head. In style.css.php is the following:
      <?php header("Content-type: text/css; charset: UTF-8"); header("Charset:utf-8"); if ($homepage->hero_image) { echo <<<CSS .hero { background: url($homepage->hero_image->url) no-repeat; } CSS; } ?> Because of the following RewriteCond (line 373) in the htaccess file the server sends a 403 error back when the file is requested:
      # Block access to any PHP or markup files in /site/templates/ or /site-*/templates/ RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/templates($|/|/.*\.(php|html?|tpl|inc))($|/) [NC,OR] (My htaccess file is @version 3.0 and @htaccessVersion 301)
      This is how I thought I could fix that (based on these answers on stack overflow) but it does not work:
      # Block access to any PHP or markup files in /site/templates/ or /site-*/templates/ RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/templates($|/|/((?!style\.css).)*\.(php|html?|tpl|inc))($|/) [NC,OR] I tested the rule with htacess tester and htaccess check and both worked for me, but on my site I still get a 403 instead of the file.
      I'm working on localhost, using MAMP (not sure if that's relevant).
      A bit more about what I want to do achieve specifically:
      I want to use an image as a background-image for an element, not place it as an image. This image is provided by the user via a field and can therefore change.
      I know I can achieve this like this:
      echo "<section class='hero' style='background-image: url($page->hero_image->url)'></section>"; But I would prefer a method other than inlining because of scalability and cleanliness. (I admit the extra link in the page head is not ideal either)
       
      P.s. this is my first post here, I hope it's submitted in the right forum and my explanation is clear.
    • 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".

      https://web.dev/samesite-cookies-explained/

      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 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
      derelektrischemoench
       
      //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...