user3392695
user3392695

Reputation: 21

Unable to read sessions in cake php

I have created a login functionality and everything is working fine. The only problem is that I am able to access the session in the users controller and views in user.

When I tried to access the user session in another controller, it shows me undefined.

Below is my login function code.

if ($this->Auth->login()) {
                    $user_data = $this->Auth->user();
                    $this->loadmodel('GeneralUser');
                    $result = $this->GeneralUser->find('first',array('conditions' => array('user_id' => $user_data['id'])));
                    $user_data_complete = array_merge($user_data,$result['GeneralUser']);
                    $this->Session->write('user',$user_data_complete);
                    $this->redirect('/dashboard/dashboard/');
                    $this->Session->setFlash('You are successfully logged in');
                    return $this->redirect($this->Auth->redirectUrl());
                }

The user is successfully logged in and redirected to dashboard controller where I am unable to access the sessions of then Auth user. If I go back to users/myaccount, I am able to access the session.

Strange, why am I not allowed to access the user sessions in other controllers?

Upvotes: 0

Views: 369

Answers (2)

Guillermo Mansilla
Guillermo Mansilla

Reputation: 3889

You SHOULD NOT use session_start() or any GLOBALS, if you do then there is no point in using a PHP Framework. Always stick to the conventions and avoid spaghetti code.

If you want to read a Session value in a Controller then use the Session Component, if you want to read the value of a session in a view then use the Session Helper.

Reading the documentation is my best advice: http://book.cakephp.org/2.0/en/development/sessions.html

Upvotes: 0

user3392695
user3392695

Reputation: 21

It got fixed..I was using the normal php session_start() in the top of the controller which was causing the issue.

Upvotes: -1

Related Questions