简体   繁体   中英

PHP session data lost after button redirect

I am creating a website where users login and their data is stored in a PHP session. On page 1 there is a button that links the user to a different page on the same site:

<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.

var_dump($_SESSION); returns "array(0) { }".

The session data is transferring when I redirect with php, but not with a button. 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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