tags:

views:

275

answers:

1

Hi,

I am using Sessions in Cakephp . I am having 3 controllers .In the User controller while logged in , i have wrote the User id in the Session variable .

But when i try to read it in the other controller i am getting the value of the Session variable.WHy so???

Edit :

In my users controller

i am writing the User id of the person who have logged in as

   function login()
   {


 $this->Session->write('userId',$this->User->loginUser($this->data));
 $this->User->data=$this->data;

 if (!$this->User->validates())
     {
      $this->Flash('Please enter valid inputs','/main' );
      return; 
     }
 if($this->Session->read('userId')>0){

                    $this->Session->setFlash('Login Successful');
  $this->redirect('/main/home');
  break;

 }
 else{
  $this->flash('Username and password do not match.','/main');

 }

}

And in my forms controller i am trying to read the session variable user id like..,

  <?php
  class FormsController extends AppController 
  {

var $name = 'Forms';
var $helpers=array('Html','Ajax','Javascript','Form');
var $components = array( 'RequestHandler','Autocomplete');
var $uses=array('Form','User','Attribute','Result','Invite','Share','Choice');


 function index() {}


     function design() 
     {
         $userId=$this->Session->read('userId');echo $userId;
         $this->data['Form']['created_by']=$this->Session->read('userId');

            $userName=$this->Form->findUserName($this->data);
            $this->set('userName',$userName);
     }
}

But this User id is not displaying the session variable what i have wrote....

Why its happening..

A: 
  1. You might want to look into using the Cake built-in AuthComponent if you haven't already.

  2. Have you actually tested the return value of $this->User->loginUser()?

  3. Can you save anything else in the Session at all?

  4. See if you can get any hints out of this question.

deceze