php - Problems with getFlash and setFlash in Yii


Ok, I know what I'm about to say is going to be super vague, but hear me out:

I want to use flash messages because I like the idea of them, but they don't seem to always work for me.

Here is my workflow:

  1. User creates a group on page A
  2. I send an email notification to members of the new group
  3. I set the flash message "Success!"
  4. I redirect the user to the newly created group and display the "Success!" flash message.

The problem is after I redirect the user, the flash messages are no longer in the $_SESSION. To make this even more strange, if I take out the step where I send email notifications, it DOES work. WTF?

Has anyone else experienced similar issues with flash messages? Does anyone have any insight into why sending the email would affect the flash messages?

I think at this point I'm just going to stick with setState and manually remove it after displaying them.




Now You can set a property autoUpdateFlash of CWebUser component to false. In this case flash message will be removed only after usage.




How is the email being sent? I know using the Yii Mail extension (a swiftmailer wrapper I think) in "debug" mode uses the setFlash method, so perhaps that's wiping out the array somehow...?

The "flash" variables are updated by the init() method of CWebUser, so if there is an extra call to CWebUser it's possible that's what is clearing out the flash variables.

Considering that you set the flash AFTER the email though, I'm not sure how that would happen. My only other guess besides the "debug mode" thing (not a problem if you are just using the PHP mail methods though) is that sending mail is triggering an extra redirect somewhere.

If you have a PHP debugger like XDebug installed I would set a breakpoint in the getFlash and setFlash code and get a look at what is happening that way.

Good luck!

People are also looking for solutions to the problem: bash - what is the best way to run shell script (with root privilege) via php?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.