【问题标题】:error in reading session variable in cakephp在 cakephp 中读取会话变量时出错
【发布时间】:2009-08-04 09:02:59
【问题描述】:

我在 Cakephp 中使用会话。我有 3 个控制器。在登录时的用户控制器中,我在 Session 变量中写入了用户 ID。

但是当我尝试在另一个控制器中读取它时,我得到了 Session 变量的值。为什么这样???

编辑:

在我的用户控制器中

我正在写登录用户的用户 ID

   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');

    }

} 

在我的表单控制器中,我正在尝试读取会话变量用户 ID,例如..,

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

但是这个用户 ID 没有显示我写的会话变量......

为什么会这样..

【问题讨论】:

  • 你可以发布任何代码...?您是在使用 Cake Auth 功能还是推出自己的功能...?
  • 我已将代码添加到帖子中

标签: cakephp


【解决方案1】:
  1. 如果您还没有使用 Cake 内置 AuthComponent,您可能想考虑一下。

  2. 你实际测试过$this-&gt;User-&gt;loginUser()的返回值吗?

  3. 你能在 Session 中保存任何其他内容吗?

  4. 看看你能不能从this question得到任何提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2013-08-03
    • 2015-12-18
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多