I know this is an amateurish question but I need to echo a session variable then destroy the variable.
I've tried this:
if(isset($_SESSION['m'])){
echo($_SESSION['m']);
unset($_SESSION['m']);
}
I've also tried session_destroy();
and i've tried to unset/destroy the session at the end of the page (after closing HTML tag)
The problem i'm having is the session is being destroyed before it is echoed. I've use the code above before without any problems. Is there anyway to do what I need to do, or do I have to find a different solution?
Thanks in advance :)
I know it's a bit late but recently I encountered this problem and I'll answer it for anyone who needs it.
I'm pretty sure you did a redirection using the header method to the same page that did the unset of the session. Something along these lines:
<?php
if(condition)
$_SESSION = "foo";
header("Location: $self");
?>
<html>
...
<?php echo $_SESSION["foo"]; unset($_SESSION["foo"]); ?>
...
The problem with header is that it doesn't stop the execution of the script (as it seems) and therefore the script continued to execute and unset the session.
The solution is to add exit(0) (or any flag you may need) after the header redirection, like this:
<?php
if(condition)
$_SESSION = "foo";
header("Location: $self");
exit(0);
?>
It's indeed a very obscure problem and a time consuming one as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With