【问题标题】:Cant read cakephp session unless I write in the same controller除非我在同一个控制器中写入,否则无法读取 cakephp 会话
【发布时间】:2012-08-28 03:42:27
【问题描述】:

除非我在要读取的控制器的开头写入,否则我无法读取我的会话数据。 示例如下:

public function login(){
$this->Session->write('Facebook.accessToken','This now works superb');
    //TODO: Implement check to see if logged in user has the same session user id.
    require_once(APP . 'Vendor' . DS .'facebook.php');
    $facebook = new Facebook(array(
    'appId'  => 'REMOVED',
    'secret' => 'REMOVED'
    ));
    $user = $facebook->getUser();
    try {
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://www.facebook.com/REMOVED'));
        $this->set('loginUrl',$loginUrl);
    }

    if(!isset($loginUrl)){
    // means that we are authenticated, so check for login with user id. If logged in redirect to event-list. If login failed display join form.    
        if($user = $this->User->findById($user_profile['id'])){
            if($this->Auth->login($user['User'])){
                $this->redirect('/users/dash');
            }
        }else{
            $this->Session->write('Facebook.accessToken2',$facebook->getAccessToken());
            $this->redirect(array('controller'=>'users','action'=>'add'));
            }
    }
}


public function add(){
    debug($this->Session->read());
    $this->Session->write('testing','value');
    debug($this->Session->read());
    }

如果我删除 $this->Session->write('Facebook.accessToken','This now works superb');,控制器将不会写入 Facebook.accessToken2。

【问题讨论】:

    标签: php session cakephp


    【解决方案1】:

    在您的 AppController 中:

    public $components = array('Session');
    function beforeFilter()
    {
        $this->Session->write('Facebook.accessToken','This now works superb');
    }
    

    在您的本地控制器中:

    function beforeFilter()
    {
        parent::beforeFilter();
    }
    

    【讨论】:

      【解决方案2】:

      你的控制器中有 Session 组件吗?

          $components[] = 'Session';
          //or
          public $components = array('Session');
      

      另外,这只是在这个认证控制器的登录动作中还是在其他控制器中?

      【讨论】:

      • 是的,它在我的AppController 中。这是我唯一的登录操作(也是目前唯一的控制器)。
      • 我应该看看 add() 动作吗?由于您试图在写入之前读取它,因此很明显它是该状态下的未知变量。此外, $this->Session->read() 需要您尝试读取的变量的名称。看看食谱吧。
      • $this->Session->read() - “返回会话中 $name 的值。如果 $name 为 null,则将返回整个会话。” login redirects 到 add 因此我正在尝试读取在login 中设置的数据,然后在向其中添加新数据后再次读取。添加也包含在上面的帖子中。
      【解决方案3】:

      我建议在启用调试器和 XDebug 的情况下逐步执行代码,以确保遵循您期望的代码路径。

      Netbeans 和 Eclipse 可以为您或任何其他具有调试单步执行功能的 php 编辑器/ide 执行此操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-02
        • 2011-05-14
        • 2011-05-13
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多