CodeIgniter Forums
Basic Login System - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Basic Login System (/thread-23368.html)



Basic Login System - El Forum - 10-08-2009

[eluser]Unknown[/eluser]
Hi all, I'm starting off developing a cms, but there's a few questions i had:

1.i know from a view, you would just use set_value ( 'username' ) to output the value back out, but in the controller, do i use set_value as well?

2.how would you set an error message to show up without creating a callback that would inevitably run when you form_validation->run();? I see form validation as a 2 step validation: first validate without the db, then check against the db ( see code )

Code:
<?php
class Login extends Controller {
    function index() {
        $this->load->helper ( 'form' );
        $this->load->library ( 'form_validation' );
        
        $this->form_validation->set_rules ( 'username', 'Username', 'trim|required|xss_clean' );
        $this->form_validation->set_rules ( 'password', 'Password', 'trim|required|xss_clean|md5' );
        
        if ($this->form_validation->run () == FALSE) {
            $this->load->view ( 'loginview.php' );
        } else { //if the form is valid, then we'll check the database
            $this->load->model ( 'cms_model' );
            if ($this->cms_model->login ( set_value ( 'username' ), set_value ( 'password' ) )) {
                $this->load->view ( 'loginsuccess.php' );
            } else {
                //$this->form_validation->set_message('username','password mismatch);
                $this->load->view ( 'loginview.php' );
            }
        }
    }
}
?>

Thanks in advance,


Basic Login System - El Forum - 10-08-2009

[eluser]imn.codeartist[/eluser]
In your controller set your error message in session

Code:
$this->session->set_flashdata('msg','your message');

in your view

Code:
if($this->session->flashdata('msg'))
{
       echo $this->session->flashdata('msg');
}

this will set error message in the session and once it print it flushes itself