0

I have the following problem and feel that the solution is simple but after 8 hours of trying and searching, I am giving up.

I have this simple page:

<?php
// Start the session
$lifetime=600;
session_set_cookie_params($lifetime);
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Change the Yoda!</title>
</head>
<body>

<?php
// Set session variables
$_SESSION["post-data"] = $_POST;
?>
    <form action="yoda_is.php" method="POST">
        YODA IS: <input type="text" name="name">
        <input type="submit">
    </form>
</body>
</html>

Upon submit, it sends me to this page:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Who is Yoda?</title>
</head>
<body> 
    <?php
    // Echo session variables that were set on previous page 
    echo "YODA IS " . $_SESSION['post-data'] = $_POST['name'];
    ?>!
</body>
</html>

The value that you enter in the first page, is successfully being displayed on the second page.

However, once I close the browser window and revisit the second page, the value is no longer there and it returns an error.

My question is simple, what am I doing wrong / do I need to do in order for the value that I entered on the first page, to be there after I revisit the second page?

Thank you so much for your help and suggestions, in advanced.

KR MD

Linkfish
  • 55
  • 1
  • 6
  • `$_SESSION` variables generally gets emptied when browser is closed – Madhur Bhaiya Sep 14 '18 at 17:57
  • Additional: I can see in the session fie that is generated, that after I submit the value on the first page, for example I write "value1" in that field, the session file contains: post-data|value1; However, when I then close the page, re-open the page, and check the session file again, it has now been overwritten with: post-data|N; Why is the session not retaining the post data? – Linkfish Sep 14 '18 at 17:58
  • Hi @MadhurBhaiya thanks for your answer. Is there any way to keep the data? I don't want it to be emptied when the browser closes, that's why I've set a session lifetime. In my current exampe it's 600 but eventually it will be 24h. I basically just want to enter a value and have it displayed on that 2nd page for 24hrs. Thanks! – Linkfish Sep 14 '18 at 18:00
  • check https://stackoverflow.com/questions/156712/php-what-is-the-default-lifetime-of-a-session – Madhur Bhaiya Sep 14 '18 at 18:00
  • Do `ini_set('session.gc_maxlifetime', 86400);` – Madhur Bhaiya Sep 14 '18 at 18:01
  • Ehh when you revisit the second page (Without visiting the first) notice: `$_SESSION['post-data'] = $_POST['name'];` – dustytrash Sep 14 '18 at 18:02
  • 1
    This is the weirdest echo/assignment: `echo "YODA IS " . $_SESSION['post-data'] = $_POST['name'];` So you are blowing out all of `$_SESSION['post-data']` with the single value of `$_POST['name']` ... is this on purpose? – IncredibleHat Sep 14 '18 at 18:02
  • Yeah it's on purpose. It'll literally just contain one name of someone, and sit on that 2nd page for 24hrs until the next day, another person goes to the first page, enters his/her name, and so on so on. – Linkfish Sep 14 '18 at 18:03
  • 1
    What is the purpose of `$_SESSION["post-data"] = $_POST;` on the first page? – dustytrash Sep 14 '18 at 18:05
  • @dustytrash , I am trying to store the data of the POST request (so the name you fill in the field on the first page) in the superglobal of $_POST, which I am then referring to on the 2nd page. Right now it only holds one value. Obviously I could be doing it wrong :-D – Linkfish Sep 14 '18 at 18:08
  • @MadhurBhaiya ,ini_set('session.gc_maxlifetime', 86400); unfortunately didn't work for me. – Linkfish Sep 14 '18 at 18:10

1 Answers1

0

On your first page remove this:

// Set session variables
$_SESSION["post-data"] = $_POST;

On your second page use this instead:

// If the user filled out the form, set our session variable to the new value
if(isset($_POST['name']))
{
    $_SESSION['post-data'] = $_POST['name'];
}

// Echo session variable set above
echo "YODA IS " . $_SESSION['post-data'] . "!";
dustytrash
  • 1,568
  • 1
  • 10
  • 17
  • Perfect, that fixed my issue! Thanks so much. I'll review the changes more in depth to better understand what changed and how this fixed it. Thanks again good sir! – Linkfish Sep 14 '18 at 18:13