Welcome Guest, Not a member yet? Register   Sign In
Need to post twice to display flashdata message.
#1

[eluser]djalilk[/eluser]
Hi everybody,
I am new to codeIgniter and trying to create a login system, and didn't find any solution to my problem
: when I try to login, if the username or the password is wrong the flashdata message is displayed only if I submit my form twice.

here is the controller function:
Code:
function verify(){
if ($this-> input->post('username')){
$user = $this->input->post('username');
$password = $this->input->post('password');
$this->Admin->verifyAdmin($user,$password);

if(isset($_SESSION['userid'])) {
if($_SESSION['userid'] > 0){
redirect('/admin/dashboard/','refresh');
}



}
}

$data['main'] = 'admin';
$data['title'] = "Foctus | Admin Login";
$data['categories'] = $this->Category->getActiveCategories();
$this->load->vars($data);
$this->load->view('template',$data);
}

the model :

Code:
function verifyAdmin($user, $password){

$data= array();
$this->db->where('username', $user);
$this->db->where('password', $password);
$this->db->where('status', 'active');
$this->db->limit(1);
$Q = $this->db->get('admins');

if ($Q->num_rows() > 0){
$row = $Q->row_array();
$_SESSION['userid'] = $row['id'];
$_SESSION['username'] = $row['username'];

}



else{

$this->session->set_flashdata('error', 'Sorry, your username or password is incorrect!');



}

}

And the view :

Code:
<div id="pleft">
<h2> Please login to Access the Dashboard </h2>
&lt;?php
if($this->session->flashdata('error')) {
echo '<div class="message"> ';
echo '<p>'.$this->session->flashdata('error').'</p>';

echo'</div>';
}
?&gt;
&lt;?php
$userData = array('name'=>'username','id'=>'u','size'=>15);
$passwordData = array('name'=>'password','id'=>'p','size'=>15);
echo form_open("/welcome/verify/");
echo "<p> <label for='u'> Username </label> <br/> ";
echo form_input($userData)." </p> ";
echo "<p> <label for='p'> Password </label> <br/> ";
echo form_password($passwordData)." </p> ";
echo form_submit('submit','login');
echo form_close();
?&gt;
</div>

Please help me
Thank you.
#2

[eluser]Steven Richards[/eluser]
Flashdata is only available on the next request, not for the current one. You can use userdata instead or use a redirect.
#3

[eluser]djalilk[/eluser]
Thanx a lot it works with userdata Smile
I don't understand is why flashdata is used in the book "professional codeigniter" ? (wich is excelent)
#4

[eluser]Steven Richards[/eluser]
Flashdata is very useful for its intended purpose: storing a temporary piece of data to be used in the very next request. It takes care of disposing of that data for you automatically so you don't forget (and so it doesn't eat up your limited amount of cookie space if it doesn't need to be hanging around for the rest of the session).
#5

[eluser]djalilk[/eluser]
Sorry but I don't undestand, I use flashdata in the next request but I need to post my form twice to get the message Sad
#6

[eluser]TheFuzzy0ne[/eluser]
Flashdata is set, and then retrieved on the request after. Generally, you'll see flashdata being set just before a redirect is made, and then the data is pulled from the flashdata variable after.

Here's a quick example (untested):

./system/application/controllers/test.php
Code:
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }

    function index()
    {
        # We're going to set the flashdata
        $this->session->set_flashdata('message', 'This is our flash data message');

        # This isn't available right until the next request, so let's force the browser
        # to redirect to the next_method.
        redirect('test/next_method');
    }

    function next_method()
    {
        # Now we can retreive the flash data.
        echo $this->session->flashdata('message');
    }
}

I'd also like to point out that using $_SESSION is not a good idea when using CodeIgniter's session class. You'll probably encounter a lot of problems.

Hopefully this helps.




Theme © iAndrew 2016 - Forum software by © MyBB