如何在CakePHP中创建,写入和读取会话数据? [英] How do I create, write, and read session data in CakePHP?

查看:446
本文介绍了如何在CakePHP中创建,写入和读取会话数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以给我一个例子,说明如何创建Sessions和写数据到它。我已经看到了如何使用write命令将数据写入会话的语法。但是如何创建会话并检索其中的值。



在我的应用程序中,我有两个数据,form_id和user_id,需要在所有页面请求中使用。那么如何将其保存为会话变量并在应用程序中使用它?



EDIT

 函数register()
{
$ userId = $ this-> User-> registerUser($ this-> data);
$ this-> Session-> write('User.UserId',$ userId);
// echosession。$ this-> Session-> read('User.UserId');
$ this-> User-> data = $ this-> data;
if(!$ this-> User-> validates())
{
$ this-> Flash('请输入有效的输入','/ forms'
return;
}

$ this-> Flash('用户帐户创建','/ forms / homepage /'.$ userId);

}

如何使用会话变量'User。 User),而不是$ this-> Flash('User account created','/ forms / homepage /'。 $ userId )中的$ userId;

我可以在所有的视图文件中使用这个变量,因为在所有的页面请求我也传递userId?



2



我有2个控制器,用户和表单。我将userid写入用户* _controller中的会话变量。我有一个视图文件称为homepage.ctp,其行为是在forms_controller。*现在我如何使用在首页中的users_controller中定义的会话变量?对不起,如果我问愚蠢的问题。我经历了蛋糕书,但我的怀疑没有被清除。我也在尝试尝试和错误的编码方法,所以请帮助我。



编辑3



我有一个会话变量'uid',它是控制器主页操作中的用户ID。

  $ this-> Session-> write('uid',$ this-> data ['Form'] ['created_by']); 

我需要在同一控制器的设计动作方法中使用相同的变量。
当我给

  $ uid = $ this-> Session-> read('uid'); 
echouid:。$ uid;

未回显此值。



我不能在同一控制器中使用会话变量吗?

解决方案

我发现了为什么uid不是回显(编辑问题的3部分)。
这是由于一个愚蠢的错误,在结束标记后有一个空白?>在控制器。现在它工作正常。


can anyone give me an example on how to create Sessions and write data to it. I've seen syntax on how to write data to a session using write command. But how to create a session and retrieve the values in it.

In my application, I have two data, form_id and user_id that needs to be used in all the page requests. So how do I save it as a session variable and use it across the application?

EDIT

function register()
{
	$userId=$this->User->registerUser($this->data);
	$this->Session->write('User.UserId',$userId);
	//echo "session".$this->Session->read('User.UserId');
	$this->User->data=$this->data;
    if (!$this->User->validates())
    {
    $this->Flash('Please enter valid inputs','/forms' );
      return;	
    }

$this->Flash('User account created','/forms/homepage/'.$userId);            

}

How to use the session variable 'User.UserId' instead of $userId in $this->Flash('User account created','/forms/homepage/'.$userId);

And can I use this variable in all my view files,because in all the page requests I also pass the userId?

EDIT 2

I have 2 controllers,user and form. I write the userid to a session variable in the users*_controller. I have a view file called homepage.ctp,whose action is in the forms_controller.* Now how can I use the session variable defined in the users_controller in the homepage? Sorry if I am asking silly questions. I went through the cakebook,but my doubts weren't cleared. I'm also trying trial and error method of coding,so please help me.

EDIT 3

I have a session variable 'uid' which is the user id in the home page action of a controller.

       $this->Session->write('uid',$this->data['Form']['created_by']);

I need the same variable in the design action method of the same controller. When I give

             $uid=$this->Session->read('uid');
            echo "uid: ".$uid;

the value is not echoed.

Can't I use the session variable in the same controller?

解决方案

I found out the reason why the uid wasn't being echoed(edit 3 part of the question). It is due to a silly mistake, had a white space after the end tag ?> in the controller. Now it is working fine.

这篇关于如何在CakePHP中创建,写入和读取会话数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆