PHP 会话,多个页面链接到一个页面 [英] PHP session, multiple pages linking to one page

查看:82
本文介绍了PHP 会话,多个页面链接到一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我才注意到这个问题.必须有办法解决它.

Okay, so I just noticed this issue. There has to be a way around it.

示例...在页面 A.php 和页面 B.php 上,有一个指向页面 ALPHABET.php 的链接.ALPHABET.php 接收指定的变量值,具体取决于哪个页面是引用者.

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.

所有涉及的页面都有 session_start();一开始.

All pages involved have session_start(); at the beginning.

页面 A.php 有:

Page A.php has:

           <?php 
                $_SESSION['name'] = "John";
           ?>

页面 B.php 有:

Page B.php has:

           <?php 
                $_SESSION['name'] = "Jane";
           ?>

页面 ALPHABET.php 有:

Page ALPHABET.php has:

           <?php
                $personName = $_SESSION['name'];
                echo "Hello, I am ".$personName;
           ?>

我决定不关闭 ALPHABET.php 中的会话,因为我希望信息在刷新页面时仍能正确加载.如果会话已关闭,则 $_SESSION['name'] 将不存在或没有值.

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.

这一切都运行良好,直到我同时加载了 A.php 和 B.php 两个页面(通过新选项卡).我注意到当我在这些页面中的任何一个上单击指向 ALPHABET.php 的链接时,它并不总是从作为引用者的页面获取会话信息.我注意到在这种情况下,加载的最后一个页面的信息将显示在 ALPHABET.php 中,而不是我单击链接的页面.

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.

即我加载了两个页面.首先是 A.php,然后我在新选项卡中打开 B.php.我单击 A.php 中的 ALPHABET.php 链接.ALPHABET.php 加载 B.php 的信息.我认为这是因为 B.php 是最后加载的页面,因此它覆盖了来自 A.php 的所有会话数据并将其替换为自己的.

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?

推荐答案

PHP 会话存储在 cookie 中,在浏览器的所有选项卡之间共享.例如.仅仅通过使用普通会话来解决这个问题并不是一个好方法.

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.

然而,一种有效的方法是将信息存储在两个单独的变量中,然后针对每个引用者发出一个 GET 请求.

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.

例如

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

Alphabet.php

if($_GET["ref"] == "A")
    echo $_SESSION["A_name"];
else if($_GET["ref"] == "B")
    echo $_SESSION["B_name"];

这篇关于PHP 会话,多个页面链接到一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆