views:

2198

answers:

5

For reasons that elude me, a session variable that contains the customer's name will show up for a short while, and then disappear.

In the 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 have tried variations on checking to see if the session is set such as

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

Everything behaves in the same bizarre way, it will display in the view for a little while, and then disappear. Doesn't come back on closing browser or logging in again. Random small changes to that code seem to bring it back to life for a short time.

A: 

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

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

to

empty($_SESSION['customer_name'])

The only other thing I can think of is lowering your security level in /config/core.php, maybe you're losing session entirely?

dr Hannibal Lecter
+1  A: 

Use $this->Session->read() instead of direct $_SESSION calls as cakephp can be storing the session somewhere else that where native PHP is expecting it.

Also, I don't really understand what you mean by 'disappear for a little while', if you stay on the same page and just keep hitting refresh does it show/dissapear randomly ?

Closing the browser could very well be the reason fot your session to be destroyed, also do you experience that behavior on different browsers ?

What are your Session.* sessings in app/config/core.php ?

duckyflip
originally I used read and check from the Session helper, but as things became more random I abandoned it.
Jack B Nimble
A: 

This line:

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

Allows $name to equal " ", 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 for that value.

Jack B Nimble
A: 

normally the session will expire after a few minutes in cake. Look for this line in the config/core.php file:

Configure::write('Session.timeout'

and set it to like 999999999. That should solve the problem.

jimiyash
According to the comments in core.php Session.timeout isn't used if you are using the Session.security setting. which mine is set to high.
Jack B Nimble
Session.timeout is used always, as a multiplier. See comment above Security.level in core.php and you'll see the math.
dr Hannibal Lecter
A: 

Note that sometimes FireFox extensions can cause session resets. Any time the User-Agent changes, the Session is reset.

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

KevBurnsJr