views:

619

answers:

5

Hi,

I have an application with multiple regions and various incoming links. The premise, well it worked before, is that in the app_controller, I break out these incoming links and set them in the session.

So I have a huge beforeFilter() in my *app_controller* which catches these and sets two variables in the session. Viewing.region and Search.engine, no problem.

The problem arises that the session does not seem to be persistant across page requests. So for example, going to /reviews/write (userReviews/add) should have a session available which was set when the user arrived at the site. Although it seems to have vanished!

It would appear that unless $this->params is caught explicitly in the *app_controller* and a session variable written, it does not exist on other pages.

So far I have tried, swapping between storing session in 'cake' and 'php' both seem to exhibit the same behaviour. I use 'php' as a default. My Session.timeout is '120', Session.checkAgent is False and Security.level is 'low'. All of which should give enough leniency to the framework to allow sessions the most room to live!

I'm a bit stumped as to why the session seems to be either recreated or blanked when a new page is being requested. I have commented out the requestAction() calls to make sure that isn't confusing the session request object also, which doesn't seem to make a difference.

Any help would be great, as I don't have to have to recode the site to pass all the various variables via parameters in the url, as that would suck, and it's worked before, thus switching on $this->Session->read('Viewing.region') in all my code!

+1  A: 

Try setting the security setting in your /app/config/core.php file to medium or low. That solved a session problem I had.

Dan Berlyoung
I already have it set to Low
DavidYell
A: 

It would appear that unless $this->params is caught explicitly in the *app_controller* and a session variable written, it does not exist on other pages.

That sounds like the proper behavior unless you are posting data from page to page. If you want any variable to persist, it should either be set in the model (where it will persist with the association), or passed on in a function, or set in the session explicitly using the session component:

$this->Session->write('Viewing.region');

(see: http://book.cakephp.org/view/398/Methods)

On a related note, I've had most success with sessions stored in the database. Run the file from app/config and set it to db. See if that helps.

Also, do the Cake core tests for the session work?

stevenf
I am setting the session using $this->Session->write in the app_controller, but once it's been written, I would expect it to persist across pages, which it doesn't seem to be doing.
DavidYell
What happens when you run Session unit tests for Cake Core?Try dropping a beforeRender to your app_controller with a debug($this->Session);Watch for: - it should always say SessionComponent Object at the top - watch the values of the object vars from page to page. You some times see errors that aren't shown otherwise.
stevenf
A: 

i had the solution or at least that work for me

you try to pass from controller reviews action write to controller userReviews action add right???

check that your controller userReviews must end whit php tag "?>" and NO MORE SPACE

SO if you have someting like this

line
999 //more code lines
1000 ?>
1001

your session fail

you have to had this

line
999 //more code lines
1000 ?>

sorry for my bad english soo you

darkcode
You can also just leave out the ending PHP tag ?>, which will keep this from happening and is perfectly legal (quite advisable if you often include files in others). The problem arises as the space causes content to be sent to the client before headers (and therefore cookies) that might be sent in the script. This only affects sessions if a cookie is being used to mitigate the session persistence.
Dustin Fineout
A: 

Might it be this problem? Essentially, cake's session resets if the user-agent changes It's a shame that I ran into this very problem you mention a few days ago and now I cannot find the link that helped me solve it.

Also: are you using database or plain php sessions?

Adriano Varoli Piazza
A: 

I'm going to go out on a limb here without being able to look at your code, but might it be possible that your "reviews" controller (or whatever) has its own beforeFilter() and doesn't call its parent's beforeFilter() explicitly?

This has burned me before...

regulatethis