繁体   English   中英

PHP包含mysql查询,使用标头阻止查询重新执行,但是在哪里回显通知?

[英]PHP include mysql query, use header to prevent query re-execution, but where to echo out the notifications?

F5重新执行查询时的我的代码:

if (isset($_POST['addnewproject']) )
{
    include("query/pm_addnewproject.php");
    echo $emailnotificationmessage;
}

为了防止在按F5键或重新加载页面后重新执行查询,我必须修改标题并将其放在.php页面的第一个规则中(在输出任何内容之前)。 所以:

    if (isset($_POST['addnewproject']) ) 
    {
        include("query/pm_addnewproject.php");
        header("Location:pm_configureaccounting.php?edit=$_SESSION[accidsession]");

        echo $emailnotificationmessage; exit();

    }

由于标题的原因,我现在丢失了我想在页面中间某个位置回显的$ emailnotificationmessage消息。

当我仍然想回声时,现在最好的事情是什么?

现在,我将$ emailnotificationmessages更改为$ _SESSION ['emailnotificationmessage'],并在需要的地方回显它。

但是我也需要取消设置此会话,否则它将一直向用户显示。

因此,当我将echo $_SESSION['bla']放在页面中间的某个位置时,它在那里有一个值。 有用。 但是我需要清除它,以便当用户再次访问时,除非再次添加项目,否则它不会显示任何消息。 因此,我将$_SESSION['bla'] = ""; 在页面末尾,此SESSION仍然获得值"" ?! 这怎么可能?

没有简单的方法知道用户是否为F5刷新页面。

但是您可以很聪明,并设置1-2个标识符。例如(使用会话)。

$request= md5(json_encode($_REQUEST));

if(isset($_SESSION["last_request"]) && $_SESSION["last_request"] == $request){
   echo "Dont refresh page...";
} 
else{
    $_SESSION["last_request"] = $request;
}

请注意,该会话将被保存,如果用户返回并再次使用相同的值提交表单,他将获得相同的“不刷新”页面消息。

if(isset($_SESSION["last_request"])){ 
    unset($_SESSION["last_request"]); 
} 

在您的表单页面上。

好的。 您为什么不按以下方式编写代码:

<?php 

if (isset($_POST['addnewproject']) ) 
{
    include("query/pm_addnewproject.php");
    $_SESSION['emailMsg'] = $emailnotificationmessage;
    header("Location:pm_configureaccounting.php?edit=$_SESSION[accidsession]");

    exit();

}else{
    echo $_SESSION['emailMsg']; 
}

?>

因此,请在您的网址中设置一个get变量,例如

www.url.com?message=hello

在另一页上使用此

if(isset($_GET['message']) && $_GET['message'] != '') {
   echo $_GET['message'];
}

给变量分配""与根本不存在相同。 您是否要取消设置会话变量?

unset($_SESSION['emailnotificationmessage']);

您将要使用unset($_SESSION['bla'])删除此特定变量,或者使用if( empty($_SESSION['bla']) ) { }检查该变量。

好吧,我现在去做这个工作。 感谢您的快速反应!

我的规则位于页面顶部:

    if (isset($_POST['addnewproject']) ) 
    {
        include("query/pm_addnewproject.php");
        $_SESSION['emailnotificationmessage'] = $emailnotificationmessage;
        header("Location:pm_configureaccounting.php?edit=$_SESSION[accidsession]");
        exit();
    }

页面深处的某个位置:

<?php
    echo $_SESSION['emailnotificationmessage'];
    $_SESSION['emailnotificationmessage'] = "";     
?>

现在工作完美。 添加新项目后,将显示该消息,并且在会话变为空后立即显示该消息。 重新加载后,会话将保持为空,直到添加了新项目。

我想我在标头之后声明了$ _SESSION,这是愚蠢的错误,这就是为什么它一直都是空的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM