问题
Okay, so I just noticed this issue. There has to be a way around it.
Example... On page A.php and on page B.php, there is a link to page ALPHABET.php. ALPHABET.php receives specified variable values depending on which page is the referrer.
All pages involved have session_start(); at the beginning.
Page A.php has:
<?php
$_SESSION['name'] = "John";
?>
Page B.php has:
<?php
$_SESSION['name'] = "Jane";
?>
Page ALPHABET.php has:
<?php
$personName = $_SESSION['name'];
echo "Hello, I am ".$personName;
?>
I decided not to close the session in ALPHABET.php, because I want the information to still load correctly if some refreshes the page. If session were closed, then $_SESSION['name'] wouldn't exist or have a value.
This all worked fine and good, until I loaded both pages, A.php and B.php, at the same time (via new tab). I noticed that the when I click the link to ALPHABET.php on either of these pages, it doesn't always take the session info from the page that was the referrer. I noted that in this situation, the last page that loaded will have its information displayed in ALPHABET.php, instead of the page from which I clicked the link.
i.e. I load both pages up. First A.php and then I open B.php in a new tab. I click on the ALPHABET.php link inside of A.php. ALPHABET.php loads B.php's information. I assume this is because B.php was the last page to load and therefore it overwrote all session data from A.php and replaced it with its own.
Is there a fix for this?
回答1:
PHP sessions are stored in cookies, which are shared between all tabs of a browser. E.G. there's not a good way to get around this simply by using vanilla sessions.
One effective way, however, would be to store the information in two separate variables and then put a GET request that is specific to each referrer.
E.g.
A.php
$_SESSION["A_name"] = "John";
?>
<a href="Alphabet.php?ref=A">Click</a>
B.php
$_SESSION["B_name"] = "Jane";
?>
<a href="Alphabet.php?ref=B">Click</a>
Alphabet.php
if($_GET["ref"] == "A")
echo $_SESSION["A_name"];
else if($_GET["ref"] == "B")
echo $_SESSION["B_name"];
来源:https://stackoverflow.com/questions/29338262/php-session-multiple-pages-linking-to-one-page