简体   繁体   中英

Php session value lost when form is submitted

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);

    ?>

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.

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