0

I am try to develop flash message using sessions in php suppose on successfully delete query I am setting

$_SESSION["msg"]="record deleted successfully";
header("location:index.php");

and I have the following script on all pages which checks if msg variable is available it echo its value as below

if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"]))
        {
            $msg=$_SESSION["msg"];
            echo "<div class='msgbox'>".$msg."</div>";
            unset($_SESSION['msg']); //I have issue with this line.
        }

if I comment

unset($_SESSION['msg']);

message is being displayed, but with this line message is not being displayed
what am I doing wrong, or any alternative.

4
  • Litte note: If a var is not empty, it implies the fact that it exists. Commented Mar 2, 2014 at 20:32
  • there's no way that unsetting the session variable AFTER you've done your echo could prevent the session value from being displayed. PHP is not capable of time travel. Make sure that you've done session_start() every where you're dealing with the session, BEFORE you deal with the session. Commented Mar 2, 2014 at 20:32
  • Maybe irrelevant, but I see 'msg' in your unset() sentence, instead of "msg" that I see in the rest of the code. Commented Mar 2, 2014 at 20:48
  • why the extra $msg=$_SESSION["msg"]; line? Just use echo "<div class='msgbox'>".$_SESSION["msg"]."</div>"; Commented Dec 29, 2016 at 8:27

2 Answers 2

4

You are saying that you have that script on every page. So my guess is that after you make header("location:index.php"); your code continues to run - your message is displayed and unset (you don't see it because of redirect to index.php). When you are redirected to index.php your message is already unset.

Try adding exit; after header("location:index.php");.

Edit: I will add two examples with one working and one not. To test you need access test page with following link - /index.php?delete=1

In this example you will never see message. Why? Because header function does not stop code execution. After you set your session variable and set your redirect your code continues to execute. That means your message is printed and variable unset too. When code finishes only than redirect is made. Page loads and nothing is printed because session variable was unset before redirect.

<?php
session_start();
// ... some code
if ($_GET['delete']==1) {
    $_SESSION["msg"] = "record deleted successfully";
    header("location: index.php");
}
// ... some code
if (isset($_SESSION["msg"]) && !empty($_SESSION["msg"])) {
    $msg = $_SESSION["msg"];
    echo "<div class='msgbox'>" . $msg . "</div>";
    unset($_SESSION['msg']);
}
// ... some code
?>

But this code probably will work as you want. Note that I have added exit after header line. You set your message, tell that you want redirect and tell to stop script execution. After redirect your message is printed and unset as you want.

<?php
session_start();
// ... some code
if ($_GET['delete']==1) {
    $_SESSION["msg"] = "record deleted successfully";
    header("location: index.php");
    exit;
}
// ... some code
if (isset($_SESSION["msg"]) && !empty($_SESSION["msg"])) {
    $msg = $_SESSION["msg"];
    echo "<div class='msgbox'>" . $msg . "</div>";
    unset($_SESSION['msg']);
}
// ... some code
?>

You clearly said that you have that code (message printing) on all pages. If your code is similar to my example than adding exit should fix your problem.

Another problem might be that you are doing more than one redirect.

Sign up to request clarification or add additional context in comments.

2 Comments

I am setting session variable on pages from where I have to generate message, and on every page I check if there is session set so I am doing 2 tasks 1- displaying msg 2- removing session var so that do not repeat on other pages
Updated answer. If this is not case than you should provide more code to see why you don't see your message.
1

You can simply set your session empty or null instead of unset it. Just do:

$_SESSION['msg']=NULL;

Or

$_SESSION['msg']="";

1 Comment

and now I have solved it by calling a php file(which has code to remove session) through ajax at the bottom of page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.