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


store error message in session and display on another page

手里有一个场景。我有 2 页index.php and code.php.

用户首先转到"索引.php"页,然后重定向到"代码.php页",然后返回到"索引.php"页。

如果代码.php页面中发生一些错误,当它被重定向回索引页面时,我需要在 index.php 页面上显示该错误。

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

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

但是,如果我在 Index 中回显此会话消息.php页面,它每次加载页面时都会显示,但是当索引页面出现在代码.php页面之后时,我只想显示一次。 谁能说出它是如何做到的。

我认为

,通过会话显示错误消息的最佳方法是具有单独的消息文件,例如消息.php。

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

$_SESSION["错误消息"]='请重试';

步骤 2 :

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

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

$error = $_SESSION["错误消息"];

第 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 文件。 它将在您的索引.php文件中回显该div。

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

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

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

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

如果索引中的会话消息.php每次打开页面时都会显示页面,则表示会话变量"errormsg"尚未清除。

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

但是,当您执行此操作时,会出现另一个问题,错误消息不会在代码中与此代码一起显示.php:

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

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

要解决此问题:

避免多次重定向页面。

在代码中.php使用:

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

而不重定向到索引.php文件。

您可以在

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

您也可以使用该条件

<?php
session_start();
if (isset($_SESSION['variablename']))
{
 //your code
}
?>
//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' ); ?>