Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple PHP Sessions

Tags:

php

session

I am to build a PHP application for a website that already has another PHP application running on the same domain/server. My app will of course be using sessions, and I don't want my sessions to interfere with the existing app. For example if I want to use $_SESSION['username'], maybe the other app also uses $_SESSION['username'], which could be a problem. I'm not looking for an extra layer of security, I trust the application I'm sharing the host with. I just want to avoid bugs.

One way would be to do something like $_SESSION['MY_APP_NAME']['username'], but I want to know if there is an easier way.

I see on the PHP documentation that there is a function called 'session_module_name'. The name sounds good, but the docs don't really explain what it is for.

Any advice?

like image 208
Nathan H Avatar asked May 12 '09 18:05

Nathan H


People also ask

Can you have multiple sessions in PHP?

In raidenace's answer two sessions are created for all clients shared among all visitor of the website. With this answer two sessions are created for each visitor to the website.

How many sessions can PHP handle?

1000+ sessions can still be perfectly handled by standard PHP file based sessions. If you notice that is getting a problem, you can exchange the session backend easily. There are pluggable session handlers for memcached or other memory or database based storage systems.

Can we start two sessions in PHP?

The answer is "no". You cannot start multiple sessions simultaneously. And if you do, you'll get an error like "A session had already been started". You don't need to start simultaneous sessions, so long you can create multiple keys.

Can one user have multiple sessions?

From the Start screen, open MultiPoint Manager. Click the Home tab. In the Computer column, click the name of the MultiPoint Server computer, and then, in the right pane, click Edit server settings. Select the Allow one account to have multiple sessions check box, and then click OK.


2 Answers

There is an easier way: session_name.

Prior to calling session_start(); call session_name("something"); (where you change something to whatever you want it to be called).

like image 154
Powerlord Avatar answered Oct 04 '22 16:10

Powerlord


Another thing that may help you in keeping apps separate is move the session storage to another place either setting session.save_path in php.ini to a folder of your choice or calling session_save_path() before session_start().

like image 23
djn Avatar answered Oct 04 '22 16:10

djn