简体   繁体   中英

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. 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?

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.

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.

Using session_name in each application could look like this:

//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. I put a define here to show that it doesn't come out of thin air.

另一种解决方案是设置一个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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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