views:

19

answers:

2

Hi all,

I am using cakePHP 1.26. In a .ctp file, I have a few like of codes like this:

$sess = $this->Session->check('user');
if($sess){
// do soemthing
}
else{
// do soemthing then
}

But I got this error then:

Fatal error: Call to a member function check() on a non-object in /home/vol12/mysite.com/htdocs/app/views/layouts/testing1.ctp on line 10

Can I use the Check method in a .ctp file to check if a session exists?

+3  A: 

There is a session component for use in controllers, of course, that is referenced as $this->Session. What you're looking for, I think is the session helper that is available to views. This is referenced simply as $session, IIRC. I wanted to verify that (it's been a while since I had to use it), but can't find it in the docs at the moment. What I think you want is:

$sess = $session->check('user');
Rob Wilkerson
agreed.Helper calling doesn't need `$this`.
SpawnCxy
+1  A: 

I can do this in 1.2.5:

if ($session->read('Auth.User')){ ... }

see the manual: http://book.cakephp.org/view/484/Session which is brief but explicit. Note that you cannot write to the session from the view.

Leo