简体   繁体   English

按钮重定向后,PHP会话数据丢失

[英]PHP session data lost after button redirect

I am creating a website where users login and their data is stored in a PHP session. 我正在创建一个网站,用户登录时将其登录,并将他们的数据存储在PHP会话中。 On page 1 there is a button that links the user to a different page on the same site: 在页面1上,有一个按钮可将用户链接到同一站点上的其他页面:

<input id="button" type="button" value="Go to different page" onclick="window.location.replace('page2.php');">

On page 2, I am unable to access the session data from page 1. 在第2页上,我无法从第1页访问会话数据。

var_dump($_SESSION); returns "array(0) { }". 返回“ array(0){}”。

The session data is transferring when I redirect with php, but not with a button. 当我使用php重定向但不使用按钮重定向时,会话数据正在传输。 Is there a different way to redirect with a button, or am I missing something to transfer the session data? 是否有其他方法可以通过按钮重定向,还是我缺少传输会话数据的功能?

Update 更新

Page 1 relevant code 第1页相关代码

session_start();
$_SESSION['name'] = 'NAME';
<input id="button" type="button" value="Go to different page" onclick="window.location.replace('page2.php');">

Page 2 relevant code 第2页相关代码

session_start();
var_dump($_SESSION);
$name = $_SESSION['name'];

you need to start the the session on each page. 您需要在每个页面上启动会话。

session_start();

another Reason could be that you destory the session at the end of your first page. 另一个原因可能是您在第一页末尾破坏了会话。

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

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