【问题标题】:store error message in session and display on another page在会话中存储错误消息并显示在另一个页面上
【发布时间】:2023-03-27 14:26:01
【问题描述】:

我目前手头有一个场景。我有2页index.php and code.php.

用户首先进入 index.php 页面,然后被重定向到 code.php 页面并返回 index.php 页面。

如果 code.php 页面出现错误,我需要在 index.php 页面被重定向回索引页面时显示该错误。

我像这样将错误消息存储在会话中,然后将页面 bsck 重定向到 index.php 页面

$_SESSION["errormsg"]='please try again';
header("Location: index.php");

但是如果我在 index.php 页面中回显这个会话消息,它会在页面加载时显示,但是我只想要一次索引页面在 code.php 页面之后。谁能告诉它怎么做

【问题讨论】:

  • 在使用或分配会话变量之前尝试顶部的session_start();
  • 每次页面加载时使用以下条件 if(isset($_SESSION["errormsg"]) && $_SESSION["errormsg"] !== 0){ echo $_SESSION["errormsg"] ;未设置($_SESSION["errormsg"]); }
  • 尝试简单,只需添加 exit();在 header("位置:index.php");然后尝试

标签: php html session


【解决方案1】:

使用一个简单的条件来检查特定会话是否已设置。

if(isset($_SESSION["errormsg"])) {
    $error = $_SESSION["errormsg"];
    session_unset($_SESSION["errormsg"]);
} else {
    $error = "";
}

echo $error;

【讨论】:

  • Uncaught ArgumentCountError: session_unset() 需要 0 个参数,给定 1 个......是我在第三行遇到的错误
【解决方案2】:

我认为,通过会话显示错误消息的最佳方式是使用单独的消息文件,如 message.php。

第 1 步: 您只需要在 code.php 中使用错误消息设置会话。

$_SESSION["errormsg"]='请再试一次';

第 2 步:

  • 现在创建名为“message.php”的新文件。

  • 将会话错误消息存储到新变量中。

$error = $_SESSION["errormsg"];

第 3 步:

然后,取消设置会话或销毁会话。

 // remove all session variables
 session_unset(); 

 // destroy the session 
 session_destroy(); 

第四步:

  • 现在在格式化的 div 标记中回显该错误变量,以便您的错误看起来不错,也很吸引人。

<div id='alert'><div class=' alert alert-block alert-info fade in center'>$error</div></div> 
第 5 步:
  • 现在,在 index.php 文件中,有一个方法可以检查会话变量“errormsg”是否已设置。
  • 如果设置了会话变量,则立即调用 message.php 文件。它会在你的 index.php 文件中回显那个 div。

我想这会给你完整的答案。

【讨论】:

  • 替换 session_unset();未设置($_SESSION["errormsg"]);不要走你可能会取消用户会话的懒惰方式
  • 如果你只是粘贴代码会更容易。
【解决方案3】:

该问题已在 3 年前得到回答,但我认为我可以在同一主题中添加一些内容。希望对您有所帮助。

如果每次打开页面都会显示index.php页面中的会话消息,则表示会话变量“errormsg”没有被清除。

因此在显示后使用unset($_SESSSION['errormsg']); 将其删除,如上所示。

但是,当你这样做时,会出现另一个问题,在 code.php 中不会显示错误消息:

$_SESSION["errormsg"]='please try again';

header("Location: index.php");

因为当您重定向到索引页面时,会话变量在消息显示之前被清除。

解决这个问题:

避免多次重定向页面。

在code.php中使用:

$_SESSION["errormsg"]='please try again';

不重定向到 index.php 文件。

【讨论】:

    【解决方案4】:

    您可以在页面开头使用session_start(),因为您正在分配会话变量值而无需实际启动会话。

    你也可以使用条件

    <?php
    session_start();
    if (isset($_SESSION['variablename']))
    {
     //your code
    }
    ?>
    

    【讨论】:

      【解决方案5】:
      //Ensure that a session exists (just in case)
      if( !session_id() )
      {
          session_start();
      }
      

      功能

      /**
       * Function to create and display error and success messages
       * @access public
       * @param string session name
       * @param string message
       * @param string display class
       * @return string message
       */
      function flash( $name = '', $message = '', $class = 'success fadeout-message' )
      {
          //We can only do something if the name isn't empty
          if( !empty( $name ) )
          {
              //No message, create it
              if( !empty( $message ) &amp;& empty( $_SESSION[$name] ) )
              {
                  if( !empty( $_SESSION[$name] ) )
                  {
                      unset( $_SESSION[$name] );
                  }
                  if( !empty( $_SESSION[$name.'_class'] ) )
                  {
                      unset( $_SESSION[$name.'_class'] );
                  }
      
                  $_SESSION[$name] = $message;
                  $_SESSION[$name.'_class'] = $class;
              }
              //Message exists, display it
              elseif( !empty( $_SESSION[$name] ) &amp;& empty( $message ) )
              {
                  $class = !empty( $_SESSION[$name.'_class'] ) ? $_SESSION[$name.'_class'] : 'success';
                  echo '<div class="'.$class.'" id="msg-flash">'.$_SESSION[$name].'</div>';
                  unset($_SESSION[$name]);
                  unset($_SESSION[$name.'_class']);
              }
          }
      }
      
      
      //Set the first flash message with default class
      flash( 'example_message', 'This content will show up on example2.php' );
      
      //Set the second flash with an error class
      flash( 'example_class', 'This content will show up on example2.php with the error class', 'error' );
      

      显示 这 消息

      <?php flash( 'example_message' ); ?>
      <?php flash( 'example_class' ); ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-04
        • 2019-01-06
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 2013-07-30
        • 2015-11-25
        相关资源
        最近更新 更多