1

I am using Apache, PHP 5.4 and Cakephp 2.4. recently I am getting random issues with my session.

My queries are based on variables stored in the session and once every 10-15 pages I get a critical error saying that the mysql query failed because it cannot find the field in the database. This field name is stored in the session that's why I suspect it being an issue with my session settings. Moreover, when I get an error and refresh the page 2 or 3 times it finally loads and the session somehow "restores" It is an e-commerce application and shopping cart is saved in the session. Even if an item is in the shopping cart, I get that error and refresh the page, shopping cart is still saved fine so the session is not completely lost. It seems like it almost disappears for few seconds.

My cake is set up like:

Configure::write('Session', array(
    'defaults' => 'php'
));

In my php.ini I already changed

  session.save_handler = memcache 
  to 
  session.save_handler = files

But it didnt help. I also get that issue when I put a link directly in my url and there is no redirect to it, so it is not an issue with redirects losing a session that was already discussed on this forum before.

Any ideas where else I should look?

Damian
  • 461
  • 1
  • 7
  • 18
  • Check if you have enough available space on the HDD partition to write the sessions. – Cosmin Aug 14 '15 at 13:22
  • How can I do that? I have a lot of space left on the drive for sure but is there a special setting that limits a space taken by sessions only/ – Damian Aug 14 '15 at 13:32
  • There are lots of answers for that question on the internet.. really... An I also don't know the server's OS. Search for "check partition space" + OS – Cosmin Aug 14 '15 at 13:35
  • I checked my partitions and they have lots of space left. Sorry for the last comment. What I meant was that I thought there is a special php setting that limits the size of sessions that are created. For the space available on the HDD there is a lot left. P.S my system is centos 7 – Damian Aug 14 '15 at 13:46
  • the above cakephp code of session store default session time you can increase you session time you this 'timeout' => 36000 or visit this link for session http://book.cakephp.org/2.0/en/development/sessions.html – Sharma Vikram Aug 14 '15 at 14:19
  • The typical reason for random session timeouts in earlier versions of CakePHP is: 404s. Check for requests for css/js/image files that don't exist, they hammer the session invalidating it (from your perspective, at random). – AD7six Aug 14 '15 at 14:24
  • I already tried that too. Increased my timeout but the error still occurs. The worst part is that when I refresh the page (sometimes 2 or 3 times) everything goes back to normal and the session is not lost. I should add that it started happening since I upgraded PHP from 5.3 to 5.4, updated cakephp from 2.3 to 2.4. I also installed new relic quite recently but that should not have anything to do with the issue. Cakephp config and codebase did not change – Damian Aug 14 '15 at 14:25
  • AD7six thanks for the hint. I removed all 404 for images and js but I still get an error. Is it possible that there are 404 that do not leave anything in the log? Is there a way to debug the session and when/why it would be invalidated? – Damian Aug 14 '15 at 16:40
  • I should probably add that it only happens when debugging is set to 0 so in production enviroment – Damian Aug 14 '15 at 16:49
  • You may log (write) session content in a file, before the failing query. To be able to debug. – Greg Kelesidis Aug 14 '15 at 17:08
  • These logs are pretty massive (I keep quite a lot in my session). They all seem to be good yet the errors still happened. On another note I am getting lots of empty error messages in my log such as 2015-08-14 13:29:58 Error: 2015-08-14 13:29:58 Error: 2015-08-14 13:29:58 Error: 2015-08-14 13:29:58 Error: Is that normal? – Damian Aug 14 '15 at 17:31
  • Maybe it is better not to log everything, only the variables related with the issue and the time. This will assure that the problem is with the session, and not something else. – Greg Kelesidis Aug 14 '15 at 17:41
  • Do you run development and production site on the same server? – Greg Kelesidis Aug 14 '15 at 17:43
  • Yes I have dev and production on one server. I dumped the session variable to a file and got every 5th file empty (on average) I need that session var in my appcontroller beforeFilter method. Seems like every 5th time that session var is blank. Is there any way to debug what is going on? – Damian Aug 14 '15 at 18:25
  • Not sure but this may help http://stackoverflow.com/questions/31530921/cakephp-needs-to-set-session-for-particular-folders/31641988#31641988 – Greg Kelesidis Aug 14 '15 at 18:40
  • Thanks Igor but my apps are sharing the same cakephp libraries. Regardless I did change prefix in one of them but it didnt help. – Damian Aug 14 '15 at 18:53

2 Answers2

0

I had a similar problem many years ago whereby I was randomly losing session variables. It turned out that my ISP, for reasons related to load balancing, had a PHP instance running on multiple servers. At the beginning of the session the Session Variables would be saved on only one of the servers. If a request came from the browser that was directed to a different server the Session variables would not be found.

I got around the problem by placing the session_save_path('/tmp') command at the beginning of every script and then immediately running the session_start() command. I create the 'tmp' folder in my private public_html space. This solved the problem.

This was in my pre-Cake days so I haven't thought about how to solve this problem in a Cake environment but hopefully it gives you somewhere to look.

Jerry Kita
  • 25
  • 5
  • I checked and there is no load balancers before my webserver. Also in cakephp I cannot put session_start() on top of every page – Damian Aug 17 '15 at 19:40
0

So it turned out the reason was recently installed APM. Thank you for all the support

Damian
  • 461
  • 1
  • 7
  • 18