重新加载页面时使用PHP会话

use*_*632 0 php session

我第一次使用$ _SESSION.我想我得到的概念,但有些东西不起作用.这是一个需要使用POST变量加载一次的页面,然后在重新加载时(通过搜索结果翻页),记住post变量的值.这两个变量将始终设置或不设置为相同的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; };
Run Code Online (Sandbox Code Playgroud)

Eli*_*sha 7

您的POST变量有错误.它应该有_如下的下划线.

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

isset($POST['exact']) // change this
isset($_POST['exact']) // to this
Run Code Online (Sandbox Code Playgroud)

否则它将始终返回false.

session_start();在顶部使用也更好(这不是问题).

进一步阐述: $_POST是一个超全球化.除了$GLOBALS需要使用下划线$和所使用的方法之外的所有.

这些超全局变量是:

  • $ GLOBALS
  • $ _ SERVER
  • $ _GET
  • $ _ POST
  • $ _FILES
  • $ _COOKIE
  • $ _SESSION
  • $ _REQUEST
  • $ _ENV

根据手册: