简体   繁体   中英

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

My code when F5 re-executes a query:

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

To prevent query re-execution after pressing F5 or page reload I have to modify a header and place it in the first rules (before any output) of the .php page. So:

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

        echo $emailnotificationmessage; exit();

    }

Because of the header, I now lose the $emailnotificationmessage I wanted to echo out somewhere middle of the page.

What's the best thing to do now when I want to still echo this out?

Now I changed the $emailnotificationmessages to $_SESSION['emailnotificationmessage'] and echo it out where I want it.

But I need to unset this session too, else it keeps showing the user.

So when I place echo $_SESSION['bla'] somewhere middle of the page, it has a value there. It works. But I need to clear it after so when the user visits again, it wont show any message unless project is added again. So I place $_SESSION['bla'] = ""; at the end of the page, and still, this SESSION gets the value "" ?! How is this possible?

There is no easy way to know if user is F5 refreshing page..

But you can be smart and set 1-2 identifiers.. For example (using session)..

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

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

Note that this session will be saved and if user go back and again submit your form with the same values he will get the same Dont refresh page message.. To prevent that set one

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

at your Form page..

Alright. Why dont you write the code in the below way:

<?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']; 
}

?>

So set a get variable in your URL like

www.url.com?message=hello

On the other page use this

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

Assigning "" to a variable is not the same as it not existing at all. Are you trying to unset the session variable?

unset($_SESSION['emailnotificationmessage']);

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

Ok I go this working now. Thanks for the fast reactions!

My rule at the top of the page:

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

Somewhere deeper in the page:

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

Works perfect now. After adding a new project, the message is shown and immedeatly after that the session becomes empty. After reload, the session stays empty untill new project is added.

I think I declared the $_SESSION after the header, stupid mistake, that's why it was empty all the time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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