CodeIgniter Forums

Full Version: Basic Login System
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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

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,

El Forum

In your controller set your error message in session

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

in your view

       echo $this->session->flashdata('msg');

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