【问题标题】:How to read cookie in cakephp 2如何在 cakephp 2 中读取 cookie
【发布时间】:2015-06-01 22:08:59
【问题描述】:

我想在用户登录期间写入 cookie,并希望下次用户打开该页面时读取 cookie 名称并在登录前显示。以下是我使用的代码:-

var $components = array('Cookie');

function beforeFilter()
{
    $this->Cookie->name = 'LOGIN_COOKIE';
    $this->Cookie->time = time()+60*60*24*30;  //cookie expire after 30 days 
    $this->Cookie->path = '/';
    $this->Cookie->domain = 'example.com/';
    $this->Cookie->secure = false;
    $this->Cookie->key = '39lbkutg1i2l0kta6785d8qki5';
    $this->Cookie->httpOnly = true;
}

在登录功能中

$this->Cookie->write('COOKIE_USER', array(
                      'name' => $UserDetails['SystemUser']['Name'], 
                      'email' => $UserDetails['SystemUser']['email'],
                      'lastname' => $UserDetails['SystemUser']['LastName']
));

在我看来

$readCookie=$this->Cookie->read('COOKIE_USER');

但它给出了错误“缺少助手”

     Error: CookieHelper could not be found.
     Error: Create the class CookieHelper below in file: app/View/Helper/CookieHelper.php

请指导我。

【问题讨论】:

  • 为什么你正在阅读 cookie 视图?您使用了 Cookie 组件,但尝试使用 cookie 助手读取而不加载它......
  • 如果用户之前访问过我们的网站,我需要在视图中显示姓名和电子邮件。所以我必须在视图中阅读cookie。你知道如何在视图中显示姓名和电子邮件吗?
  • 是的,我知道,请看下面的答案...

标签: php cakephp cookies cakephp-2.0


【解决方案1】:

在您的控制器中执行此操作

//Write enter code here
$this->set('readCookie', $this->Cookie->write('COOKIE_USER'=> array(
                      'name' => $UserDetails['SystemUser']['Name'], 
                      'email' => $UserDetails['SystemUser']['email'],
                      'lastname' => $UserDetails['SystemUser']['LastName']
)));

//Read
$this->set('readCookie', $this->Cookie->read('COOKIE_USER'));


//write into a key
$this->Cookie->write('COOKIE_USER.name'=>'Piya');
//Read a key
$this->set('username', $this->Cookie->read('COOKIE_USER.name'));

那么在你看来

debug($readCookie);
echo $username;

如果你不需要,请评论它们

//function beforeFilter()
//{
//    $this->Cookie->name = 'LOGIN_COOKIE';
//    $this->Cookie->time = time()+60*60*24*30;  //cookie expire after 30 days 
//    $this->Cookie->path = '/';
//    $this->Cookie->domain = 'example.com/';
//    $this->Cookie->secure = false;
//    $this->Cookie->key = '39lbkutg1i2l0kta6785d8qki5';
//    $this->Cookie->httpOnly = true;
//}

look at this example

【讨论】:

  • 我已经添加了评论,但在登录时显示messgae -:- 无法修改标题信息 - 标题已发送
  • @PiyaSharma 据我所知,在视图渲染之前不应该有任何输出。检查报告的文件和行以寻找奇怪的东西。
  • 我没有坚持错误,但是问题的“关键”,它是我的安全盐,但实际上,随着 cipherSeed Works 的数字而变化! !我已经解决了。。
【解决方案2】:

在控制器中试试这个代码

 if ($auth) {
                $this->Auth->setUser($auth);
                if ($this->request->data['remember'] == 1)
                    $this->Cookie->write('User', $this->request->data);
                else
                    $this->Cookie->delete('User');
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->set(__('Invalid credentials.'), ['element' => 'default', 'params' => ['class' => 'alert alert-danger']]);
        }
        if ($this->Cookie->check('User')) {
            $this->request->data['email'] = $this->Cookie->read('User.email');
            $this->request->data['password'] = $this->Cookie->read('User.password');
            $this->request->data['remember'] = $this->Cookie->read('User.remember');
        }

【讨论】:

    【解决方案3】:

    在控制器使用时读取 cookie-

    $this->Cookie->read()
    

    在查看使用时读取 cookie-

    CookieComponent::read();
    

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2015-01-23
      相关资源
      最近更新 更多