5

I want to set a different session ID depending on what folder a user is in.

For example, I have the domain https://example.com which has the folders /app1, /app2, etc. and then multiple files inside each app folder. I would like to set one session ID to be used with all files in app1 and a different session ID to be used in app2.

Can this be done?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Kere Puki
  • 53
  • 1
  • 1
  • 3

5 Answers5

4

Set the path in the session cookie with session_set_cookie_params. Before session_start of course.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • Hi, I tried that: session_set_cookie_params(60*60*24*5, "app1", "xxx.com"); but it didnt work. It just stopped loading the rest of the PHP scripts. Do I need to edit something in php.ini? – Kere Puki Mar 29 '11 at 22:31
  • ... do this: `session_set_cookie_params(60*60*24*5, "/app1");` – Naftali Mar 29 '11 at 22:35
  • If it 'stopped loading', are you sure you didn't just cause a syntax error? Also, add a leading `/` to the path. If this doesn't work, try to `ini_set('session.cookie_path','/app1')` before a `session_start`. On a side note: you don't have to declare the domain (3rd parameter) unless you're trying to do something special with it. – Wrikken Mar 29 '11 at 22:36
  • Still nothing. Both apps are still calling the same session_id when writing and reading info from DB. Any other suggestions? – Kere Puki Mar 29 '11 at 22:48
  • 3
    Oh, and of course: don't have a 'default' session on the root `/` / site-wide. If you have one of those, you might just want to switch the `session_name` before `session_start` with `session_name('app1');` – Wrikken Mar 29 '11 at 23:21
3

If /app1 and /app2 contain completely unrelated applications, you could institute completely unrelated sessions on both by using PHP's session_name(), function. Generally, sessions work with a cookie called PHPSESSID. Instead, in /app1 you can run session_name('session_app_1') and in /app2 you can run session_name('session_app_2'). Now the sessions in the two applications will be completely unrelated.

Warning: PHP's documentation says that the session_name() function is expensive. Perhaps leave the session with the default name in the busier app, and rename it only in the quieter app.

TRiG
  • 10,148
  • 7
  • 57
  • 107
  • That solved the problem in my case. I am experimenting with two directories, each containing a different application, which must share the same session. Setting the same session save path and the same session name worked. – jim_kastrin Sep 18 '15 at 20:45
0

Setting the session_save_path to different directories will serve your purpose. For example, you could set it to a subdirectory within each dir, ie. relative path. Otherwise to share sessions between directories set session_save_path to the same full path. Works for me..

brianH
  • 11
  • 5
0

u can use the idea that the $_SESSION var is an array:

$_SESSION['app1'] = ...
$_SESSION['app2'] = ... //etc etc
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I am using arrays to store the different sessions but its just session_id always gets set to the same id regardless of the folder. – Kere Puki Mar 29 '11 at 22:33
0

I'm not sure that I understand the problem correctly, but you could try checking the path to see whether it's in app1, app2, etc., then get the part of the string you're interested in and use that to set your session, for example with $_SERVER['REQUEST_URI'] or $_SERVER['SCRIPT_NAME'].

Unless you're already doing that and your problem is that it just won't set the session?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Cristian
  • 198
  • 2
  • 13