简体   繁体   中英

Using PHP session when reloading a page

I'm using $_SESSION for the first time. I think I get the concept, but something isn't working. This is a page that need to load once with POST variables and then, when reloaded (to page through search results), remember the values of the post variables. The two variables would always be set or not set at the same tiem.

//submitted form variable definitions
if (!isset($_SESSION)){
    session_set_cookie_params(3600,"/");
    session_start();
}

if (isset($POST['word'])) { $name=$_POST['word'];   
    $_SESSION['word'] = $name; };
if (isset($POST['exact'])) { $exact=$_POST['exact'];  
    $_SESSION['exact'] = $exact; };

Your POST variables have a mistake. It should have an underscore _ like follows.

isset($POST['word']) // change this
isset($_POST['word']) // to this

isset($POST['exact']) // change this
isset($_POST['exact']) // to this

Otherwise it will always return false.

It is also better to use session_start(); at the top (It is not a problem here).

To further elaborate: $_POST is a superglobal . All except $GLOBALS require an underscore between $ and the method used.

These superglobal variables are:

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV

as per the manual:

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