简体   繁体   English

当我在页面之间移动时:我丢失了会话变量

[英]When I move between pages: I loose my session variables

I have a fairly simple PHP page which checks an account name value in a database.我有一个相当简单的 PHP 页面,它检查数据库中的帐户名值。 The plan is: if the account details are there, there then I set a login flag to true, store a couple of extra session variables and then move to another page.计划是:如果帐户详细信息在那里,那么我将登录标志设置为 true,存储几个额外的会话变量,然后移动到另一个页面。 My next page checks the content the session variables and then goes on to present some options to the user.我的下一页检查会话变量的内容,然后继续向用户呈现一些选项。

The issues is: Although the session ID and session variables get set in the first page.问题是:尽管会话 ID 和会话变量在第一页中设置。 The second page cannot see the values.第二页看不到这些值。

I can't see what I am doing wrong here:我看不到我在这里做错了什么:

  • I have looked at lots of sample code etc我看过很多示例代码等
  • Looked through a bunch of questions on StackOverflow: can't see where the issues is.看了一堆关于 StackOverflow 的问题:看不出问题出在哪里。

My code is as follows:我的代码如下:

Page 1: (I have left out the DB connection - I know this is working) there are a few If statements ahead this section which basically means I only get to this part if various fields are set.第 1 页:(我已经忽略了数据库连接 - 我知道这是有效的)本节前面有一些 If 语句,这基本上意味着如果设置了各种字段,我只会进入这部分。 As you can see from page output (1) ... I can see all of the data I need.正如您从页面输出 (1) 中看到的那样......我可以看到我需要的所有数据。

        session_start();
                echo '<p>checking $AN :' . $AN;
                $_SESSION['Status']= true;
                $_SESSION['Acct'] = $AN;
           
           // debug details - echo details to screen to check values

            $check = $_SESSION['Status'];
            $name = $_SESSION['Acct'];

            echo '<br>logon status :' . $check;
            echo '<br>logon Acct : ' . $name;
            echo '<br> session ID = :'. session_id();
            echo '<a href="/sessioncheck.php" target="_self">click here</a>';

Second page (/sessionsheck.php)第二页 (/sessionsheck.php)

$check = $_SESSION['Status'];
$name = $_SESSION['Acct'];

echo '<br>logon status :' . $check;
echo '<br>logon Acct : ' . $name;

echo '<br>session stats : ' . $_SESSION['Status'] . ' + ' . $_SESSION['acct'];
echo '<br> session ID :' . session_id();

My out is as shown here:我的输出如下所示:

for page 1 before I click the link ..... page output在我点击链接之前的第 1.....页面输出

For page 2 (session check.php) Page output对于第 2 页(会话 check.php)页面输出

I can't figure out where I have gone wrong.我想不通我哪里出错了。

  • I have no code to close the session or 'unset' the variables in either page yet.我还没有关闭会话或“取消设置”任一页面中的变量的代码。
  • There is nothing else other than the database connections.除了数据库连接之外别无他物。

I am trying to interact with the variables further down the chain ... not sure if I need to send the Session ID with the page request eg: /xyz.php?=SessionID我正在尝试与链下游的变量进行交互......不确定是否需要将会话 ID 与页面请求一起发送,例如:/xyz.php?=SessionID
Not sure if that would defeat the purpose of having actual data stored in the session on the server ?不确定这是否会破坏将实际数据存储在服务器上的会话中的目的?

Any help greatly appreciated.非常感谢任何帮助。 Thank you in advance先感谢您

You need to invoke the您需要调用

session_start();

in your check.php too.在您的check.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM