在会话中存储错误消息并显示在另一个页面上

store error message in session and display on another page

我手头有一个场景 present.I 有 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 页面之后时,我只想要一次。谁能告诉我怎么做

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

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

echo $error;

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

也可以使用条件

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

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

第 1 步: 您只需在 code.php.

中设置带有错误消息的会话

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

第 2 步:

  • 现在创建名称为 "message.php" 的新文件。

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

$error = $_SESSION["errormsg"];

第 3 步:

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

 // remove all session variables
 session_unset(); 

 // destroy the session 
 session_destroy(); 

第 4 步:

  • 现在在格式化的 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 .

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

//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' ); ?>

这个问题在 3 年前就已经回答了,但我想我可以在同一主题中添加一些内容。希望对你有帮助。

如果index.php页面中的session message每次打开页面都显示,说明session变量"errormsg"还没有被清除。

因此按照上面的建议使用unset($_SESSSION['errormsg']);将其删除。

但是,当您这样做时,会出现另一个问题,错误消息将不会与 code.php 中的此代码一起显示:

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

header("Location: index.php");

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

解决这个问题:

避免多次重定向页面。

在code.php中使用:

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

不重定向到 index.php 文件。