Krystian
Krystian

Reputation: 458

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?

Upvotes: 0

Views: 545

Answers (3)

Agung Gugiaji
Agung Gugiaji

Reputation: 121

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

Upvotes: 0

Ivaldi
Ivaldi

Reputation: 660

Another solution is to set a cookie path (before session_start()).

Upvotes: 0

chiborg
chiborg

Reputation: 28104

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.

Upvotes: 1

Related Questions