Cakephp - session variable keeps acting strange

For reasons that elude me, the session variable containing the client's name will show up briefly and then disappear.

In app_controller.php: beforeFilter ()

if (isset($_SESSION['customer_name']) == false  || 
    strlen($_SESSION['customer_name']) == 0)
{
  $customer = $this->Customer->read(null, $auth['User']['customer_id']);
  $name = $customer['Customer']['fname'] . " " . $customer['Customer']['lname'];
  $this->Session->write('customer_name', $name);
  $this->set('name', $this->Session->read('customer_name'));
}
else
{
  $this->set('name', $this->Session->read('customer_name'));
}

      

I tried the validation options to check if a session is established like

if ($this->Session->check('customer_name') == false)

      

Everything behaves in the same strange way that it will appear in the view for a while and then disappear. Will not revert to close browser or login again. Random small changes to this code seem to bring it to life for a short time.

+1


a source to share


6 answers


This line:

$name = $customer['Customer']['fname'] . " " . $customer['Customer']['lname'];

      



Lets $ name be "", which means it has a strlen of 1 and is not empty. Not sure why it didn't get the customer data, but now I can check this value.

0


a source


Use $ this-> Session-> read () instead of direct calls to $ _SESSION, as cakephp can store the session somewhere else, which is where native PHP expects it.

Also, I really don't understand what you mean by "fade out for a while", if you stay on the same page and just keep updating, does it show random display / fade out?



Closing your browser could very well be the reason your session gets destroyed, are you also experiencing this behavior across browsers?

What are your Session.*

sessings in app / config / core.php?

+2


a source


This solved our problem:

if($this->Session->check('customer_name'))

+1


a source


Not sure what the problem is, but I would start by removing the $ _SESSION calls. I would also change

strlen($_SESSION['customer_name']) == 0

      

to

empty($_SESSION['customer_name'])

      

The only thing I can think of is to lower the security level in /config/core.php, maybe you will completely lose the session?

0


a source


usually the session will expire after a few minutes in the cake. Find this line in your config / core.php file:

Setup :: records ('Session.timeout'

and set it to 999999999. That should fix the problem.

0


a source


Please note that sometimes FireFox extensions can cause a session reset. Each time the User-Agent changes, the session is reset.

I recently ran into this issue using a FireBug extension called FirePHP:
http://blog.kevburnsjr.com/cakephp-session-error-user-agent-must-be-consistent

0


a source







All Articles