Different session value of same variable in different folders

Hi All,

I have two folders in my www: test1/index.php and test2/index.php
code test1/index.php


<?php
@session_start();
if(isset($_SESSION['name']) && $_SESSION['name'] != ''){
	echo "Name=" . $_SESSION['name'];
} else {
	$_SESSION['name'] = 'Foo';
}
?>
<html>
<head>
<script type="text/javascript">
function fun(){
	window.open("../test2/index.php","NewWindow", "width=600");
}
</script>
</head>
<body>
<input type="button" value="Click" onclick="fun();" />
</body>
</html>

Code in test2/index.php


<?php
@session_start();
if(isset($_SESSION['name']) && $_SESSION['name'] != ''){
	echo "Name=" . $_SESSION['name'];
} else {
	$_SESSION['name'] = 'Bar';
}
?>

Through browser when accessing test1/index.php and clicking on ‘Click’ button,
o/p=> Name=“Foo”

Is it possible

  1. If I access test1, o/p=> Foo
  2. If I access test2 (as clicking on ‘Click’ button), o/p=> Bar

Your help will be highly appreciated.

Thanks in advance.

So you either want a new session started from the pop-up window (controlled by the browser) or you want to namespace your sessions so you can pragmatically separate your session data between the two windows.

Is that correct?

The only solution I know of is to prefix your session variable names with the directory you are in.

<?php
$directory = basename(dirname($_SERVER['PHP_SELF']));
@session_start();
if(isset($_SESSION[$directory.'name']) && $_SESSION[$directory.'name'] != ''){
    echo "Name=" . $_SESSION[$directory.'name'];
} else {
    $_SESSION[$directory.'name'] = 'Bar';
}
?>

Thanks cpradio.

But in my folder, there are so many session variable (which I may not be knowing) in so many files.

You can also try setting the session_id(‘place value here’), but I’m not sure how that works across threads (if that needs to be unique to each thread, etc)