0

So my issue is that I'm setting a message in a session var to carry over in a page redirect. And then setting the var to an empty string so it doesn't redisplay everytime. Like so:

if ($successMsgs || !empty($_SESSION['msg_success'])) {
 $success_block[] = '<ul id="success-block">';
 foreach($successMsgs as $success) {
  $success_block[] = '<li>'.$success.'</li>';
 }
 if (!empty($_SESSION['msg_success'])) {
  $success_block[]='<li>'.$_SESSION['msg_success'].'</li>';
  $_SESSION['msg_success']='';
 }
 $success_block[] = '</ul>';
 $success_block = implode('',$success_block);
}

The problem is that the clearing of the session var seems to have a retro-active effect so the message never gets displayed. It only works if I take out the line that re-sets it to an empty string. I'm thinking there's something about when session vars are evaluated that I don't understand?

Karl
  • 147
  • 1
  • 1
  • 6

3 Answers3

2

Except for the freedom to define functions and classes after invoking them, there is definitely nothing retro-active in PHP. Session variables will be available after the session_start() command. Unsetting a session variable inside the block won't have an effect in the code before it occurs.

Your problem must have to do with something else - maybe the page gets called twice, or a header redirect takes place?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • The session var gets set in a redirect function like so: function redirect($to,$msg){ $_SESSION['msg_success']=$msg; header('Location:'.$to); } and this function gets called on form submit if validation passes – Karl Aug 29 '10 at 17:01
  • @Karl the session is initialized using `session_start()` in both instances? – Pekka Aug 29 '10 at 17:02
  • yep, session_start() occurs before the other code on every page load...would I also need to invoke it inside the redirect function? – Karl Aug 29 '10 at 17:06
  • @Karl no, it should be fine. Can you do a `print_r($_SESSION); die();` on the target page to see whether the values are really stored? – Pekka Aug 29 '10 at 17:09
  • Karl, @Pekka is correct here... it looks like something else is probably at play. Are you saying that if you comment out line 8 above (`$_SESSION['msg_success']='';`) the code works? – Josh Aug 29 '10 at 17:35
  • yep, if I comment out line 8, it works fine...the page loading twice would cause this behavior, but I don't see how that would be happening – Karl Aug 29 '10 at 17:40
  • @Karl, to debug better, write something like `$_SESSION['test_cnt'] = 0;` and after your line 8, write `$_SESSION['test_cnt']++;` then where you are displaying the success message, display this `$_SESSION['test_cnt']` also. – Sandeepan Nath Aug 29 '10 at 17:54
  • when I submit the form and get redirected, the count goes up by 2, and the count increment occurs beneath the redirect...so I guess, my next question is: Does php continue to run code that's beneath a redirect before actually redirecting? – Karl Aug 29 '10 at 18:12
  • so my code sequence looks like this: 1. redirect + session msg setting function invoked 2. output of session msg + unsetting of session msg is step 2 getting run despite the redirect above it? – Karl Aug 29 '10 at 18:32
  • @Karl, I am not very sure about that, I think if that happens there is something wrong going on because header should ideally redirect and not execute code beneath. I remember facing such situations but I just put an `exit()` right after the redirect, because the situation demanded me to finish the task... and it worked for me. Just check if that works, although that is not a good solution. Would like to know about the correct solution though. Anybody? – Sandeepan Nath Aug 29 '10 at 18:37
  • @sandeepan, This works exactly as expected. The browser performs the redirect based on the header information sent by PHP. `header()` sends data to the browser, it doesn't, nor should it, redirect script execution. As far as PHP is concerned, there's no practical difference between sending a location header and a Content-Type header. – jasonbar Aug 29 '10 at 18:45
  • @jasonbar, agreed that you are correct. code execution still continue after header redirect. I was wrong. Thanks. – Sandeepan Nath Aug 29 '10 at 18:58
  • Yes, I can confirm that definitely, PHP does NOT stop executing after a redirect `header()`. You must call `exit()` – Josh Aug 29 '10 at 19:23
1

It turned out that the code beneath the redirect was getting run, before actually redirecting. The solution was simply to add an exit to the redirect function.

Karl
  • 147
  • 1
  • 1
  • 6
  • Haha like I said, that is not a good solution. Would like to know about the correct solution though. Anybody? – Sandeepan Nath Aug 29 '10 at 18:40
  • I was wrong, code executes after header redirect, but still, the fact that your counter reaches 2 indicates that the success message setting code executes twice. You may think of correcting that. – Sandeepan Nath Aug 29 '10 at 19:02
  • @sandeepan: Why isn't this a good solution? It seems like it's exactly what he wants... – Josh Aug 29 '10 at 19:24
  • @Josh I feel the best coding practice here would be to put the redirect inside an if condition and other things inside an else condition, so that after the redirect there is nothing to execute. no need to put an exit then. Thats why, I now remember, this issue has not bugged me everytime. I faced the issue when I did not put the redirect under proper condition. Nowadays, I put redirects under proper if conditions – Sandeepan Nath Aug 29 '10 at 19:48
  • @sandeepan: Okay, I'm with you now. That's a common practice. I think his solution is OK, but I can completely understand your point as well. I'll bet you [prefer a single return statement per function](http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement), too? :-) – Josh Aug 29 '10 at 20:54
0

well, the only possibility i can think of is that you are calling this piece of coding twice. and in the first call it doesn't get printed. maybe you are redirecting twice for some reason...

Hugo Mota
  • 11,200
  • 9
  • 42
  • 60