0

I have three index.php files in three different folders where each folder (except the base) is a child in another folder. Each file should have its own $_SESSION array, and should not be overwritten by another path/file.

I just assumed that this would be the case, however, created the following test script, and found this is not the case.

I've also discovered that the session cookie associated with each path is domain "myDomain.com" and path "/".

What is the best way to ensure files in a directory tree each have their own session?

/var/www/html/main.php

<ul>
    <li><a href="/testing/cookies">base</a></li>
    <li><a href="/testing/cookies/path1">path1</a></li>
    <li><a href="/testing/cookies/path1/path2">path2</a></li>
</ul>
<?php
session_start();
echo('<h1>'.$name.'</h1>');
echo('<pre>'.print_r($_SESSION,1).'</pre>');
$_SESSION['test']=$name;
?>

/var/www/html/index.php

<?php
$name='/';
require('main.php')
?>

/var/www/html/path1/index.php

<?php
$name='/path1';
require('../main.php')
?>

/var/www/html/path1/path2/index.php

<?php
$name='/path1/path2';
require('../../main.php')
?>
user1032531
  • 24,767
  • 68
  • 217
  • 387

1 Answers1

0

You can set alternate path by calling session_save_path($path) before you call session_start()

more info:

http://php.net/manual/ru/function.session-save-path.php

v.sheldeshov
  • 178
  • 6
  • Yes, I can set the path, but then I will have three cookies with the same name on path2, two on path1, and one on base. I've read on http://stackoverflow.com/questions/4056306/how-to-handle-multiple-cookies-with-the-same-name that this could cause inconsistency on some browsers. I am thinking `session_name()` might be more appropriate, but am not sure. – user1032531 Nov 11 '14 at 16:21
  • Good idea! Use pair session_name() + session_save_path($path) before session_start() – v.sheldeshov Nov 11 '14 at 16:38