Flash messages and redirect

Hi,

I am considerably new to Yii, and the whole MVC architecture, for that matter, most computer architectures. Anyway, i have figured out most of the details, i am right now having a problem, and i have searched the forum thoroughly and didnt find any solutions.

In short my problem is after a set a flash message and i redirect the flash message doesn’t appear properly

Here are some code to make my point more clear.

I have a method as such in my controller.




public function actionFacebook(){

      /* Facebook graph and authentication process..*/

      if($fb_success){

             Yii::app()->user->setFlash('notification','Your have been successfully logged in by facebook!');

             $this->redirect(array('site/index'));

      }

      else

             die('and go to hell <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/biggrin.gif' class='bbc_emoticon' alt=':D' />');

}



I hope the idea is clear enough, i am looking for a good solution to it, try to be as conventional as possible.

I dont mind work arounds, i tried using Yii::app()->user->session[‘message’] and destroying the session, but with no successs…

cheers,

mursi

Are you doing something in your views to display the message?

http://www.yiiframework.com/wiki/21/

Excerpt:


<?php if(Yii::app()->user->hasFlash('success')):?>

    <div class="info">

        <?php echo Yii::app()->user->getFlash('success'); ?>

    </div>

<?php endif; ?>

yes ofcourse, thats elementary. however if statement fails with hasFlash ( returns false )

Is the session stored in a cookie or appended as HTTP GET parameter?

If the latter, the session id won’t be passed by redirect.

Cf. http://php.net/manual/en/function.header.php

vs.




class CHttpRequest extends CApplicationComponent

{

        ...

	public function redirect($url,$terminate=true,$statusCode=302)

	{

		if(strpos($url,'/')===0)

			$url=$this->getHostInfo().$url;

		header('Location: '.$url, true, $statusCode);

		if($terminate)

			Yii::app()->end();

	}

        ...

}



I am not sure, but i think cookies, i did javascript:alert(document.cookie); and that gave me PHPSESSID=[somethign,something…]

Any other ideas??

common, anyone there?? i really need some help here

In the layout try an


echo Yii::app()->user->setFlash('notification');

If it shows the message, means that flash message was correctly setted, check why the display is not working.

If you have not the message, check if the code


 Yii::app()->user->setFlash('notification','Your have been successfully logged in by facebook!');

Has been executed.

The flash messages have a counter, which starts at one. Every time Yii starts a new page request, the counter gets decremented by one. When the counter reaches zero, that flash message gets deleted. If you haven’t displayed it by then, you never can. This is meant to make the flash messages available on the current and next requests, but it doesn’t seem to work the way it was intended.

When you redirect, you are ending the current request and starting a new one. This means the flash counters count down by one and the messages get deleted. This happens before your controller runs. Therefore you can’t display the flash messages after a redirect.

Yii 1.1.7 and later include a property CWebUser::autoUpdateFlash which can be set to false to turn off the flash message counter. Messages will then stay in the session until you display them.

Edit the ‘components’=>‘user’ section of your config file like this:




'user'=>array(

	'allowAutoLogin'=>false,

	'class'=>'CWebUser',

	'autoUpdateFlash' => false, // add this line to disable the flash counter

),



Thanks Keith. That fixed my problem. Though i am still a bit confused.

For me the same problem was coming up, but only in Firefox browser (v4 and v5). I’m using Yii 1.1.8 and used the exact code as the yiiblog demo. While the demo worked fine, my code didnt work until i added the above lines mentioned by you. I wonder why?

This appears to be fixed in v1.1.8.

I tried this solution, too. Unfortunately, it seems like this solution does not work when logging out - probably because the cookie that stores information like that is being restored.

Is that a bug or can I work around that somehow? How do you guys display flash messages when logging out?

thanks it worked for me in 1.1.8 :rolleyes:

1.1.8 have the same "problem"

Still not fixed in 1.1.10 …

Thanks

Keith Dechant thanks!

Thank you,

It works for me in 1.1.13

I am using Yii 1.1.13 and have the same problem. But this solution didn’t fix it for me :(

I am using Yii-user module for login, and after login I set the flash. And after the redirect, the flash message no longer exists.

use this code hope it will work for you beacause its working ok for me…

<?php if(Yii::app()->user->hasFlash(‘success’)):?>

&lt;div class=&quot;info&quot;&gt;


    &lt;?php echo Yii::app()-&gt;user-&gt;getFlash('success'); ?&gt;


&lt;/div&gt;

<?php endif; ?>

you share your code here will try to help you beacuse its working for me well

Hi there!

I still have this problem. Using Yii 1.1.14, I have set the option


autoUpdateFlash

to


false

, but still no flash message after a redirect.

Thanks for the help.

Cheers!

In my case the reason was in incorrect use of session_start() and session_write_close() somewhere in the code above the setFlash();