Welcome Guest, Not a member yet? Register   Sign In
flashdata don't work
#1

[eluser]Unknown[/eluser]
Hi!

I've a litte trouble with set_flashdata/flashdata function that maybe you guys can help me. After set the userdatas, I verify if the user is disabled (deleted) in the Admin controller. If it's "Desativado", then I redirect to the login function, in the Principal controller, where I show the flashdata.

here is my code

Admin Controller
Code:
class Admin extends CI_Controller {
    function _logado(){
        $nivel = $this->session->userdata('nivel');
        if (!isset($nivel)){
            return false;
        }
        if ($nivel == 'Desativado'){
            return false;
        }
        return true;
    }
    
    public function __construct() {
        parent::__construct();
        if (!$this->_logado()){
            $this->session->set_flashdata('failed', 'Você não tem acesso a essa área');
            redirect('login', 'refresh');
        }

    }
    
    function index(){
        if ($this->_logado())
            $this->load->view('admin/index');
        
    }

Principal Controller
Code:
class Principal extends CI_Controller {

    public function __construct(){
        parent::__construct();
    }
    public function index()
    {
        $this->load->view('principal/index');
    }
    
    function login(){
        $this->load->view('admin/login');
    }
//further code

Login view
Code:
//further html code
                    <?php echo $this->session->flashdata('failed');?>
                    <table>
                        <tr>
                            <td>Login:</td>
                            <td>&lt;?php echo form_input('nome',set_value('nome'),"id='nome'");?&gt;</td>
                        </tr>
                        <tr>
                            <td>Senha:</td>
                            <td>&lt;?php echo form_password('senha','',"id='senha'");?&gt;</td>
                        </tr>
                        <tr>
                            <td>&lt;?php echo form_submit('','Login');?&gt; </td>
                            <td> </td>
                        </tr>
                    </table>
                    &lt;?php echo form_close()?&gt;
//further html code

The session class is autoloaded. 'login' is in the routes.php file ($route['login'] = "principal/login";).

I've tried to show the flashdata after I've set it, but nothing appeared. What am I doing wrong??

I'm using the version 2.0.2 of CI, Apache 2.2.11 and PHP 5.3.0

Thanks in advance
#2

[eluser]InsiteFX[/eluser]
Because you are making multiple calls so you need to use keep_falshdata
Code:
$this->session->set_flashdata('failed', 'Você não tem acesso a essa área');

// change:
$this->session->set_flashdata('failed', 'Você não tem acesso a essa área');
$this->session->keep_flashdata('failed', 'Você não tem acesso a essa área');

InsiteFX
#3

[eluser]Unknown[/eluser]
Thanks for the answer, InsideFX, but it didn't work.

It seems that I can't set up new information in the session from Admin controller and uses it in the Principal controller.

I've tried change the flashdata to userdata in the Admin controller, but even so it didn't show the message in the view. But when I tried to set a userdata in Principal controller, it did show the message! And the flashdata still insists in don't appear...




Theme © iAndrew 2016 - Forum software by © MyBB