• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to post twice to display flashdata message.

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:
function verify(){
if ($this-> input->post('username')){
$user = $this->input->post('username');
$password = $this->input->post('password');

if(isset($_SESSION['userid'])) {
if($_SESSION['userid'] > 0){


$data['main'] = 'admin';
$data['title'] = "Foctus | Admin Login";
$data['categories'] = $this->Category->getActiveCategories();

the model :

function verifyAdmin($user, $password){

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

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



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



And the view :

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

$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();

Please help me
Thank you.

[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.

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)

[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).

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

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):

class Test extends Controller
    function Test()

    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.

    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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.