Display message for first page load only

I want to display a thank you note after adding a comment only for loading the first page ... The comment form is processed using an external php file and redirected back to the page. I would like to display the message after the redirect only ... What would be the best way to do this with php?

+5
source share
5 answers

Assuming you have access to an external php file that processes the file, you can do something similar to the following in the processing file:

$_SESSION['flashMessage'] = 'Thank you for posting.';
header("Location: your-page.php');

Then add the following to the redirect page:

if ($_SESSION['flashMessage']) {
    echo $_SESSION['flashMessage'];
    $_SESSION['flashMessage'] = NULL;
}
+5
source

Save the message in the session. Display it and just turn off the session variable.

+3

, :

if($success)
{
    $_SESSION['userMsg'] = "<p>Your comment has been added. Thank you.</p>";
}

/ ( , ):

if($_SESSION['userMsg'] != '')
{
    print $_SESSION['userMsg'];
    unset($_SESSION['userMsg'];
}

, session_start()

+1

When you redirect to send a variable through the $ _GET array something like this:

header("LOCATION: index.php?msg=1" );

If checking the index if $ _GET ['msg'] == 1 then display your message

0
source

You might want to apply the PRG pattern. Basically, you post a comment, and the server responds to the client to redirect to your page with additional information in the query line, as Vadim claimed.

"Elegant", session and functional.

0
source

All Articles