开发者

Can not get session in cakePHP

I have problem with session in cakephp.I 开发者_开发技巧have one file chat.php that is in webroot folder but when I run that file with ajax I could not find session which is created from chat.ctp file. so anyone has idea how to get session in third party file in cakePHP.

can I write session any .ctp file ?

Thanks.please help me.


Yes, it's totally possible.

In APP/config/core.php, find the "Session.cookie" config value:

Configure::write('Session.cookie', 'CAKEPHP');

Then, in your external file, just set the same session name when you start your session:

session_name('CAKEPHP');
session_start();
print_r($_SESSION);

I have heard that this is a spotty thing but for me, the solution above has worked in two projects so far.


I'm assuming you're using the Session component to write the session variable and then trying to read it from a regular 'ol php file outside of cake's scope using the $_SESSION variable.

As you have figure out, this is not guaranteed to work properly. The best way to handle this would be to integrate your chat.php file into cake, using a controller, action and view.

If this for some strange reason isn't possible, you would need to import all the files responsible for setting up cake and instantiate the session component and use that to read from your session. I'd strongly recommend going with way #1.


I don't think it's a good idea to use the third party session in CakePHP since Cake has already done that very well.And I believe the best place to make some sessions is controller,not view.See Session in Cakephp's cookbook and Ajax helper of Cake.Probably they would help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜