简体   繁体   English

两个应用程序中的PHP会话

[英]PHP sessions in two application

I've got question to you. 我有问题要问你。 I have got two project in PHP that uses captcha, and write it to session. 我有两个使用验证码的PHP项目,并将其写入会话。 My question is, if I fire first application, that saves captcha to $_SESSION['code'] and then I start second application where I saves captcha to same variable, then the first value will be overwritten by second, or PHP will create two independent sessions? 我的问题是,如果我启动第一个应用程序,则将验证码保存到$_SESSION['code'] ,然后启动第二个应用程序,将验证码保存到同一变量,那么第一个值将被第二个覆盖,或者PHP将创建两个独立会议?

Normally, each application will override the session variables on the same server. 通常,每个应用程序将覆盖同一服务器上的会话变量。

To avoid this, you can either namespace your sessions or use the session_name function. 为避免这种情况,您可以为会话命名空间或使用session_name函数。

You can namespace manually, by setting $_SESSION['app1']['code'] and $_SESSION['app2']['code'] or by using a session abstraction library like the one from Symfony or Zend Framework. 您可以通过设置$_SESSION['app1']['code']$_SESSION['app2']['code']或使用会话抽象库(例如Symfony或Zend Framework中的库)来手动命名空间。

Using session_name in each application could look like this: 在每个应用程序中使用session_name可能看起来像这样:

//Other init stuff here
define('APPLICATION_ID', "MY_UNIQUE_ID_1");
session_name(APPLICATION_ID);
session_start(); 

You'll have to change the unique id in some configuration file for each application. 您必须在每个应用程序的某些配置文件中更改唯一ID。 I put a define here to show that it doesn't come out of thin air. 我在这里define一个define ,以表明它并非凭空产生的。

另一种解决方案是设置一个cookie路径 (在session_start()之前)。

In my experieance, If both of your application is on same virtual directory then it will be overwriten. 以我的经验,如果您的两个应用程序都在同一个虚拟目录中,那么它将被覆盖。 So if you do not want to overwrite each other use different session variable. 因此,如果您不想互相覆盖,请使用其他会话变量。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM