Community & Support

Custom Session Variables and Drupal Caching

I've got an issue across a few of my clients websites where we have used custom session variables in the page.tpl.php file which seemingly worked in previous Drupal versions, but it seems that in recent versions when Drupal performance caching is enabled that the values for the session variables are not retained. Once we turn off the performance caching there isn't a problem.

Any thoughts or suggestions?

Comments

I have the same issue as

I have the same issue as well. Anyone got any info?

Triverse Designs, your online solutions made easy

Triverse Designs, your online solutions made easy

ditto

I'm dealing with this issue. With normal caching enabled session variables aren't retaining their values for anonymous users. Disabling caching returns things to normal.

It's weird that more people aren't talking about this.

-------------------------------------------------------

"The sting in any rebuke is the truth." - Benjamin Franklin

they just havent made

they just havent made complex sites :)
i have same problem. and i used custom session for URL basis and when cached my whole site have gone crayze and url's got mixed. thinking of tryin cookies now to see does it make difference. was also thinking of using boost module but wondering what could there be used if here sessions are not working.

alternatives

I tried switching to cookies, but for some reason (it's been awhile) they didn't meet my needs.

I also looked at using the Session API module, but opted not to use it either.

The quick fix at the time was to disable caching, which works fine small low-traffic sites. However this issue really needs a concrete and long-term solution. I will revisit the whole thing the next time the need arises.

-------------------------------------------------------

"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain

i found some other post,

i found some other post, mentioning cookies with same problem so i didnt even try it. ofcourse on small site u dont need caching. well in fact you dont need drupal much. :-(
its logical for me, if drupal caches whole page it would cache variables and not recreate them anymore. what drupal needs then is more granular caching system. i would reckon that session API wouldnt help much as again all variables would be cached and not changed according to user. If somebody knows different please tell.

Depending on your application

Depending on your application needs you might be able to do something like Session Favorites.

-------------------------------------------------------

"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain

thanx man, all best in 2009.

thanx man, all best in 2009. nice find, hope i can make it work without problems with caching.

well not so good in my case

well not so good in my case after all :-(

anonymous user sessions

I'm not exactly sure what you're trying to do, but Session Favorites illustrates (with the use of Session API) how to track anonymous user sessions. It might be easier to simply have your users register. You can make it simple by turning of email validation, giving them instant access.

-------------------------------------------------------

"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain

Nope

I tried the "easier" route and in my case it adds too many other complexities to manage and disabling email validation wasn't a snap, either, but I forget the details.

What if we use a php snippet to clear the cache immediately prior to the event that queries the session, although that feels like a sledgehammer for this thumbtack?

Then in my case i would

Then in my case i would clear cache all the time and probably just slow down server and site, not a good idea.

I have this issue as well.

I have this issue as well. There should be a hook to allow disabling of cache for specific page templates which need to modify custom session variables. For instance, I need to disable caching for my success page redirect which gets and deletes a custom session variable set by the form/module the page is redirected from. With caching enabled, that variable is not being deleted.

Same issue here, with Session Favorites module.

Anyone found a solution yet? I'll try to come up with a hack that forces drupal not to use the cached version of a page when the user has a certain session variable set and see how it goes...

Newbie trying

I am new to Drupal. Just some thoughts.

What's in cache are static pages, meaning no or minimum server side execution. So I guess the best way to have session variables work with caching is to store these variables in cookies and access them by JavaScript.

it's fixed by using normal caching mode

1. select site performance from site setting admin
2. at caching mode select Normal (recommended for production sites, no side effects).
3. Save.

It's works well

Watch out for the hook

Hello,

You should also take care of which hook you are using your session variable. Some hooks will just be cached and not executed each time if you enable caching on your site.

The code you write in page.tpl.php will be executed once and cached and that whatever the level of caching. What you need to do to be able to play with session variable with cache enabled is to create and module and write all session variable code in the mymodule_boot() hook. The code you write there will be executed each time a page is loaded in normal mode. In aggressive mode this code will be cached as well and ineffective!!!

I wrote an article about which hook to use here :

http://www.ravelsoft.com/blog/2010/session-variable-drupal

Good luck with your issue.

Félix Delval