0

I have very simple script that should set a sessionvalue and after pressing submit button the same page should reload and remember the previously set sessionvalue.

Please kindly help to solve this problem! Thank you!

CODE WAS UPDATED 10 MIN LATER.

<?php
    session_start();

    //  if (session_status() == PHP_SESSION_NONE) {
    // 
    //  session_start();
    //  echo date("Y-m-d H:i:s");
    //  echo 'Session has been now started.';
    //  var_dump($_SESSION);
    //  echo '<br>';
    // 
    //  } else
    //  {
    // 
    //  echo 'Session was started already earlier.';
    //  var_dump($_SESSION);
    //  echo '<br>';
    // 
    //  } 

    if (isset($_SESSION['sessionvariable']))
    {

    echo 'Effort one1: We have a value for sessionvariable. <br>';

    } else {

    echo 'Effort one1: We dont have a value for sessionvariable. <br>';

    }

    $_SESSION['sessionvariable'] = 3030303;

    echo 'We have now set the value for sessionvariable:';
    echo $_SESSION['sessionvariable'];
    echo '<br>';


    echo '<form action="sessiontest.php" method="POST" enctype="multipart/form-data">';
    echo 'Give some input: <input type="text" name="naming" value="something"> <input type="submit" value="upload">';
    echo '</form>';
    echo '<br>';

    var_dump($_SESSION);

    ?>
wensgal24
  • 11
  • 3

1 Answers1

7

You never set a session variable. The syntax to set a session variable is:

$_SESSION['sessionvariable'] = 3030303;

What you are doing is $sessionvariable = 3030303; which sets a non-session variable which is lost when the page is terminated. Just using the word session in your variable name does not make it a session variable.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • **Thanks** for kind advice, I have now updated my code based on your advice. However, **still the main problem remains**: when reloading the page with the submit button the sessionvalue variable is forgotten. Please still help me to solve this!! :) – wensgal24 Sep 10 '15 at 15:44
  • If you overwrite the session value before you try to read its value you will not get expected results. Also, you have whitespace before your PHP code, that will break things. – John Conde Sep 10 '15 at 15:47
  • Thanks for info... overwriting...? Just could you still very kindly more precisely describe how to modify my code... I have searched solutions for this already two full days. Thanks. – wensgal24 Sep 10 '15 at 15:50
  • Start by getting rid of the whitespace before the ` – John Conde Sep 10 '15 at 15:52
  • Thanks, I checked that the original code (before copy-pasting here) did not have whitespace... So perhaps still something else is causing problem... I really appreciate any further suggestions. – wensgal24 Sep 10 '15 at 15:56
  • Follow the steps in [this answer](http://stackoverflow.com/a/19692734/250259) and see if it helps. – John Conde Sep 10 '15 at 15:57
  • Thank you John Conde! Your help solved my problem. Accidentally the cookies setting in my browser was off and putting them on helped to get expected results, i.e. session value was not lost anymore at reloading page. – wensgal24 Sep 10 '15 at 16:04