0

我已經創建了登錄功能,一切工作正常。唯一的問題是我能夠訪問用戶控制器中的會話和用戶視圖。無法讀取蛋糕中的會話php

當我試圖訪問另一個控制器中的用戶會話時,它顯示我未定義。

以下是我的登錄功能代碼。

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()); 
       } 

用戶已成功登錄並重定向到儀表板控制器,我無法訪問Auth用戶的會話。如果我回到用戶/ myaccount,我可以訪問會話。

奇怪,爲什麼我不允許訪問其他控制器中的用戶會話?

回答

-1

它得到了修復..我正在使用正常的php session_start()在導致問題的控制器的頂部。

+1

這是不正確的做法。你可以使用Session helper&component來做這件事。 – 2014-09-08 08:53:57

0

你不應該使用session_start()或任何GLOBALS,如果你這樣做,那麼使用PHP框架沒有意義。始終遵守公約規定並避免意大利麪代碼。

如果要讀取Controller中的Session值,則使用會話組件,如果要讀取視圖中會話的值,請使用會話助手。

閱讀文檔是我最好的建議:http://book.cakephp.org/2.0/en/development/sessions.html