Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:my session_变量显示页面刷新的时间_Php_Session Variables - Fatal编程技术网

PHP:my session_变量显示页面刷新的时间

PHP:my session_变量显示页面刷新的时间,php,session-variables,Php,Session Variables,我知道这是一件很简单的事情,但我没有意识到这一点。我在同一页面上有一个注册表单的php代码,当任何条件与代码匹配时,都会显示一些会话变量 代码结构如下所示: <?php session_start(); if(isset($_POST['signup']) { if(condition) { $_SESSION['err1']="string"; } else

我知道这是一件很简单的事情,但我没有意识到这一点。我在同一页面上有一个注册表单的php代码,当任何条件与代码匹配时,都会显示一些会话变量

代码结构如下所示:

      <?php
         session_start();
         if(isset($_POST['signup'])
       {
      if(condition)
       {
          $_SESSION['err1']="string";
       }
     else
       {
           $_SESSION['err2']="string";
       }
     }

     ?>

                 //HTML form
    <?php if(isset($_SESSION['err1']) {?>
    <li><?php echo $_SESSION['err1'];}?></li>

    <?php if(isset($_SESSION['err2']) {?>
    <li><?php echo $_SESSION['err2'];}?></li>


                 //rest of the form

只需在回显后取消设置会话变量即可

<li><?php echo $_SESSION['err1'];} unset($_SESSION['err1']); ?></li>

  • 这可能是因为您没有清空会话变量

    在两个HTTP请求之间,会话保持在服务器上(只需在每个请求时重新加载)

    因此,如果您在第一次呼叫时在会话['error1']
    上放置消息,它将显示该消息。然后,在第二次加载时,如果您在
    $\u会话['error2']
    上放置消息,您还将收到
    error1
    消息,因为会话保留了您的数据


    显示表单后,应清空所有会话消息。这是一个使用会话来回显错误的糟糕示例

    我在php开始时做了很多次

    $errors = array(); // make a empty array errors before the conditional statements
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['Submit'])) {
          //handle your POST variable
          if(condition1){ 
              $errors[] = "some error";
          } 
          if(condition2) {
              $errors[] = "some another error";
          }
          //more conditions
    
         if (!empty($errors)) {
               //process your form data if there is no errro
         } else {
            //display back your form along with Errors
            if(isset($errors) && !empty($errors)) {
    
                 foreach($errors as $error) {
                    echo "<p class = 'error'>" . $error . "</p>";
                  }
            }
            <form action = "" method = "POST">
               //your form elements
            </form>
         }
    }
    
    $errors=array();//在条件语句之前生成空数组错误
    如果($\u服务器['REQUEST\u METHOD']='POST'&&isset($\u POST['Submit'])){
    //处理POST变量
    如果(条件1){
    $errors[]=“某些错误”;
    } 
    如果(条件2){
    $errors[]=“其他一些错误”;
    }
    //更多条件
    如果(!empty($errors)){
    //如果没有错误,请处理表单数据
    }否则{
    //显示表单以及错误
    if(设置($errors)&&!空($errors)){
    foreach($errors作为$error){
    echo“

    ”$error.

    ”; } } //您的表单元素 } }
    在php页面的第一行,您可以编写 您可以尝试if条件之间的三行中的任意一行

    if(isset($_SESSION))
    {
    unset($_SESSION); 
    unregister($_SESSION['variable-name']) // try this also 
    session_destroy();  //try this also 
    }
    

    我不明白这个问题。什么形式?我没有看到任何表单。这可能是因为您没有清空会话变量。在两个HTTP请求之间,会话保持在服务器上(只需在每个请求时重新加载)。因此,如果您在第一次呼叫时在会话['error1']上放置消息,它将显示该消息。然后,在第二次加载时,如果您在
    $\u会话['error2']
    上放置消息,您还将收到
    error1
    消息,因为会话保留了您的数据。显示表单后,您应该清空所有会话消息。我应该使用session\u destroy还是使用其他功能清空它?您可以使用
    unset($\u session['error1'])
    清空并对所有条目执行此操作,或者尝试重置整个会话
    $\u session=array()并打分。