Instead of changing the logic of the system, a simple solution is for you to change the name of cookie according to the folder, before of session_start()
.
In doing so, you have fully independent but simultaneous sessions:
<?php
// inicio do bloco de teste
$independentes = array( 'app1', 'app2' );
$caminho = explode( '/', $_SERVER['PATH_INFO'] );
$appnumber = array_search( $caminho[1], $independentes );
session_name( 'PHPSID_'.( $appnumber === false ? 0 : $appnumber + 1 ) );
// fim do bloco de teste
session_start();
Basically we are taking the second item of the path divided by the bars (the first is empty, because the PATH_INFO
begins with /
), locating his position in a array with the name of the folders, and adding their position to the name of the cookie session, making each situation have a fully independent session.
PS: If you are not using CGI or Apache, change the PATH_INFO
for REQUEST_URI
.
In case, it pays to create a include with the lines of the test block, and give a require_once()
on your session pages. By doing this, you can test as many different folders as you want with independent sessions simultaneously. Simply put the root folder name of each application in place of app1
and app2
in the array.
Example:
aplicação 0 em http://127.0.0.1/...
aplicação 1 em http://127.0.0.1/teste_a/...
aplicação 2 em http://127.0.0.1/teste_b/...
aplicação 3 em http://127.0.0.1/teste_c/...
Setup:
$independentes = array( 'teste_a', 'teste_b', 'teste_c' );
Anything out of the way teste_a
, teste_b
and teste_c
, or in paths that are not in the list, will be considered as part of the standard application (0
).
Reusing in several pages:
To apply the solution on multiple pages, you can save this file as session_start.php, for example:
<?php
$independentes = array( 'app1', 'app2' );
$caminho = explode( '/', $_SERVER['PATH_INFO'] );
$appnumber = array_search( $caminho[1], $independentes );
session_name( 'PHPSID_'.( $appnumber === false ? 0 : $appnumber + 1 ) );
session_start();
And simply use with require_once()
in place of the session_start()
original:
<?php
require_once( 'session_start.php' );
// ... resto do seu código ... //
It would be much better to separate the local "lodgings" to make life easier. Even pq this type of "organization" in folders usually does not organize things much if in the definitive hosting the application is staying at the root of the site (usually people use relative paths where it should be absolute precisely because of the folders). Two most obvious solutions for testing are "hosting" at different ports, or better yet, creating test Urls on the system’s "hosts" pointing to 127.0.0.1 (eg: app1.Runo and app2.).
– Bacco