如何在常规PHP脚本中继续CakePHP 3会话? [英] How to continue a CakePHP 3 session in a regular PHP script?

查看:72
本文介绍了如何在常规PHP脚本中继续CakePHP 3会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况如下。我有一个cakephp项目和一个在同一台服务器上运行的单独的纯PHP脚本。
当我使用客户端浏览器连接到cakephp项目时,它将建立一个应有的会话。

My situation is the following. I have a cakephp project and a seperated plain php script running on the same server. When I use my client browser to connect to the cakephp project, it builds up a session as it should.

现在,我想继续使用我的纯PHP脚本。再次,我使用相同的客户端浏览器来访问纯PHP脚本(因此,请求元数据应该相同并且应该识别会话),并将cakephp session选项设置为PHP。

Now I want to continue the session data with my plain php script. Again I use the same client browser to access the plain php script (so the request meta data should be the same and the session should be recognized) and I set cakephp session option to PHP.

'Session' => [
  'defaults' => 'php',
 ],

但是,我找不到如何继续该会话的方法在普通的php脚本上。
我本来以为我的普通php脚本的以下两行会发挥作用:

However, I cant find out how to continue the session on the plain php script. I would have assumed the following two lines of my plain php script would do the magic:

session_start(); 
echo json_encode($_SESSION);

亲切的问候,
Marius

Kind regards, Marius

推荐答案

CakePHPs PHP会话默认设置(如所有内置默认设置)确实会更改cookie的名称/会话的名称( session.name INI设置)到 CAKEPHP

CakePHPs PHP session defaults (like all built-in defaults) do change the name of the cookie / the name of the session (session.name INI setting) to CAKEPHP:

https://github.com/cakephp/cakephp/blob/3.5.3/src/Network/ Session.php#L133-L138

https://github.com/cakephp/cakephp/blob/3.5.3/src/Network/Session.php#L133-L138

因此,您必须更改它以匹配您的原始PHP应用程序使用的默认值(即最有可能是 PHPSESSID ,即PHP默认值):

So you either have to change that to match the defaults used by your vanilla PHP app (which is most probably PHPSESSID, ie the PHP default):

'Session' => [
    'defaults' => 'php',
    'cookie' => session_name(), // would use the PHP default
],
// ...

或更改后一个应用程序以使用为CakePHP应用程序配置的名称:

or change the latter app to use the name configured for your CakePHP application:

session_name('CAKEPHP');
session_start();
// ...

还要确保 session.cookie_path session.cookie_domain 配置涵盖了您的两个应用程序位置。

Also make sure that the session.cookie_path and session.cookie_domain configuration covers both of your applications locations.

另请参见

  • Cookbook > Sessions > Session Configuration
  • Cookbook > Sessions > Setting ini directives

这篇关于如何在常规PHP脚本中继续CakePHP 3会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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