Welcome Guest, Not a member yet? Register   Sign In
Flashdata problem
#1

[eluser]Ngulo[/eluser]
hi i'm trying to set a simple flashdata
controller:
Code:
$this->session->set_flashdata('error','this is an error');
redirect(admin/home);

view:
Code:
<?php echo $this->session->flashdata('error');?>

but it doesn't works .... realy don't know what to do....i would like to set flashdata and not userdata ....please help me peoplesssss Big Grin
#2

[eluser]adamp1[/eluser]
Have you checked in the cookie/database table to see if the value is getting saved there? If so what state is it in when you have loaded the view (i.e. is its state set to old/new).

Reason I ask is a flashdata value can only be retrieved if its state is old. You cannot read flashdata which is new.
#3

[eluser]Ngulo[/eluser]
oh god .....i think i'm mistaking.... i didn't know that flashdata uses a DB table to store them self Sad

i always thought that they were contained into the $_SESSION array.... does it?

my knowledge was that:

userdata= $_SESSION(long time session set setting)
flashdata= $_SESSION(short time session data setting)

right?

sorry for my bad english Sad

so i always need a table DB for storing my flashdata? Sad
#4

[eluser]erik.brannstrom[/eluser]
No, storing sessions in a database is not the default behavior so - while recommended for sensitive data - it is not at all obligatory.

The thing about flashdata is that it is only saved for ONE request. So if you set the data in a controller and then redirects to another controller method, only this second method will have access to the data. Thus, if you redirect again, the data will be lost (unless you specify it to be kept). Also, as adamp1 said, it cannot be directly accessed from the first method either, since there has to be one request between the setting and getting of the data.

Does that help you at all?
#5

[eluser]Ngulo[/eluser]
Ok thanks for clarification

So the redirect, removes the flashdata, I understand ...??

If I use a flashdata after a login page instead of doing a
Code:
$this->session->set_flashdata('error','this is an error');
redirect ('home')

I should make
Code:
$data['error1']=$this->session->set_flashdata('error','this is an error');
$this-> load-> view ('home', $data)

for reading the flashdata?

right?

Smile
#6

[eluser]erik.brannstrom[/eluser]
No, the first one is right, however if there is a problem this probably depends on how your home controller looks like. Does the index method contain another redirect? Perhaps you could paste the code of that method.
#7

[eluser]Ngulo[/eluser]
hi man,thanks for supporting me

the code are these into the controllers

Controller home.php:
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Controller
{
    function Home()
    {
        parent::Controller();
       //se esiste la sessione utente amministratore
        if($this->session->userdata('administrator'))
                {redirect('admin/admin');}
    }
    function index()
    {
        
                $this->load->view('admin/home_view');
    }
}

?>

Controller login.php :
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Controller
{
    function index()
    {
        //filtro dati da input in htmlentites
        $post_username=htmlentities($this->input->post('username'));
        $post_password=htmlentities($this->input->post('password'));

        $this->form_validation->set_rules('username','required');
        $this->form_validation->set_rules('password','required');

        if($post_username == "asd" and $post_password=="asd"
                && $this->form_validation->run() == TRUE)
            {
            //setto session administrator
            $this->session->set_userdata('administrator',''.$post_username);
            redirect('admin/admin');

            }
            else{
                $this->session->set_flashdata('error_login','Non sei autorizzato per questa area');
                redirect('admin/home');

                }

    }
}

?>

this to say that my home.php contain another redirect...so is this the problem?how to resolve that?

really thanks
p.s: do you think is the code right any suggestion also on that?

thanks again
#8

[eluser]erik.brannstrom[/eluser]
I can't really wrap my head around your code.. Are you using re-routing of some sort or do you have an Admin controller that you haven't posted? I see that you set the flashdata when a user fails to login, but I don't understand where he is redirected, since if the user is redirected to the Home controller he will immediately be redirected once again to admin/admin. Also, it might be of interest to see how you display the data in your view.

In regard to your code in general I would say that you are sort of misusing the form validation, however if this works just as you want it to then feel free to continue. Or rather, if this is how you want it to work, there isn't a need to even use the validation library. Smile
#9

[eluser]Ngulo[/eluser]
Basically i have this configuration in my system directory view and controller folders
Code:
-System
  -Application
    -Controllers
       Admin-
          -Home.php (default)
           -Login.php (login action)
          -Admin.php
    -Views
       Admin-
       -Home_view.php (here is the login form)
       -Admin_view.php

the views are home_view.php
Code:
<div id="form">
&lt;?php
    echo form_open('admin/login');?&gt;
   <strong>Username</strong><br>
    &lt;input type="text" name="username" size="40" maxlength="30" /&gt;&lt;br>
    
       <strong> Password</strong><br>
    &lt;input type="text" name="password" size="40" maxlength="30" /&gt;
    
    <br></br>
    <br>
    &lt;input type="submit" value="Login"/&gt;
    &lt;/form&gt;
    &lt;?php echo $this->session->flashdata('errore_login');?&gt;
    </div>

admin_view.php
Code:
&lt;?php if($this->session->userdata('messaggio_upload')){echo $this->session->userdata('messaggio_upload');} ?&gt;

the fact is that I only let users with root username and password then you're right, the library validation form may not serve



home.php serves on the redirect if the session exists ('administrator') to skip the form and enter directly into admin.php

do not know if I said, excuse my English Tongue

thank you
#10

[eluser]Ngulo[/eluser]
i have forgotten the admin.php controller Tongue

Code:
&lt;?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends Controller
{
    function Admin()
    {
        parent::Controller();
        //se non esiste la sessione utente amministratore
        if(! $this->session->userdata('administrator'))
        {redirect('admin/home');}
    }
    function index()
    {
    $data['query'] = $this->db->get('joomla_templx');
    
        $this->load->view('admin/admin_view',$data);
    }
    
    
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB