-3

I would like to clear my session vqriqble "flash' using the unset function but now I can't print my flash message because the unset function is called before my previous code. It is called in my header template : header.php wich is called in every page at the beginning of the code.

This is the code :

<?php if(isset($_SESSION['flash'])): ?>
  <?php foreach($_SESSION['flash'] as $type => $message): ?>
    <div class="alert alert-<?= $type; ?> bk-fullwidth-alert">
      <p><?= $message; ?></p>
    </div>
  <?php endforeach; ?>
  <?php unset($_SESSION['flash']); ?>
<?php endif; ?>

Thank you guys for your help !

1 Answers1

1

Try this. The problem is that unset() was inside the loop, so the variable was being unset-ed on the first loop execution.

<?php if(isset($_SESSION['flash'])): ?>
  <?php foreach($_SESSION['flash'] as $type => $message): ?>
    <div class="alert alert-<?= $type; ?> bk-fullwidth-alert">
      <p><?= $message; ?></p>
    </div>
  <?php endforeach; ?>
  <?php unset($_SESSION['flash']); ?>
<?php endif; ?>

Edit: try this one

<?php
    if(isset($_SESSION['flash'])) {
        $flash = $_SESSION['flash'];
        unset($_SESSION['flash']);
        foreach($flash as $type => $message) {
            ?>
            <div class="alert alert-<?=$type?> bk-fullwidth-alert">
                <p><?=$message?></p>
            </div>
            <?php
        }
    }
?>
  • Sorry, I inverted two lines because I was testing the code but it's not working... –  Jan 04 '16 at 23:18
  • If I remove the unset function my code is working correctly. –  Jan 04 '16 at 23:18
  • That's weird, this code is working fine for me. However, as a workaround, you could do `$flash = $_SESSION['flash']; unset($_SESSION['flash']);` and then using only $flash variable instead of $_SESSION['flash'] to see if it works. Now, [just for curiousity](http://php.net/manual/pt_BR/function.unset.php#40539), is your register_globals on or off on php.ini? – Jefrey Sobreira Santos Jan 04 '16 at 23:25
  • Here's the code above, which you can run online: http://phpfiddle.org/main/code/v1yz-w5dn is this code producing similar results when you run it on your server? – Jefrey Sobreira Santos Jan 04 '16 at 23:34
  • As the manpage says for "register_globals" on PHP official site : This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. –  Jan 05 '16 at 00:10
  • Yeah but in earlier versions than 5.4 it can bring some problems. But the code I hosted on phpfiddle works on your box? – Jefrey Sobreira Santos Jan 05 '16 at 00:15
  • Yep the code is working. So does it come from my other source files ? –  Jan 05 '16 at 13:46
  • It might be, but it's hard to say without actually reading them. I edited my answer above. Does the new code work for you? – Jefrey Sobreira Santos Jan 05 '16 at 15:33
  • Yep it works thx !! but why i can't use the $_SESSION var ? that is my question ? –  Jan 06 '16 at 11:04
  • That's weird, as it worked for me. But it might be a PHP bug too, in the version you've installed, as seen [here](https://bugs.php.net/bug.php?id=36646) and [here](http://www.justskins.com/forums/36646-com-foreach-_session-171248.html). So the new code is just a workaround for it. – Jefrey Sobreira Santos Jan 07 '16 at 16:08
  • So I think that is the problem cuz in the tutorial guy is doing like me. –  Jan 11 '16 at 09:01