#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,677
    Rep Power
    171

    A login script. Create session if valid password.


    Hi guys;

    I like to learn a bit on structuring my controllers. Would you please give me some tips on how I can improve this controller? Is it normal for controllers to look messy? Or I should seperate everything in a seperate controller?
    PHP Code:
    class Log_in extends CI_Controller
        
    {
            
            public function 
    check()
                {
                    if(
    $this->session->userdata('logged')!=TRUE)    
                        {
                            
    $this->load->view('login');    
                        }
                }
            public function 
    do_log()
                {
                    
    $this->form_validation->set_rules('password''Password''required|callback_password_check');    
                    
                    if (
    $this->form_validation->run() == FALSE)
                        {
                            
    $this->check();
                        }
                        else
                        {
                            
    $newdata = array('logged_in' => TRUE);
                            
    $this->session->set_userdata($newdata);
                            
    redirect(base_url('products'), 'refresh');
                        }
                }
            public function 
    password_check($password=NULL)
                {
                    
    $this->load->model('model_sa');
                    
                    if (!
    $this->model_sa->login($password))
                        {
                            
    $this->form_validation->set_message('password_check''Invalid password');
                            return 
    FALSE;
                        }
                    else
                        {
                            return 
    TRUE;
                        }
                }    
        } 
    Model
    PHP Code:
    public function login($password=NULL)
            {
                
    $this->db->select('id');
                
    $this->db->from('systematic_attraction_login');
                
    $this->db->where('password'sha1($password));
                
    $query $this->db->get();
                return 
    $query->num_rows;
            } 
    **I understand sha1 is a bad choice**

    Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,034
    Rep Power
    535
    Looks pretty clean to me. One observation is password_check() should probably do something and not just respond yes/no. And your controller appears to be appropriately dedicated to its scope of work and therefore you should not further separate tasks into separate controllers. Put all login related tasks into the Log_in class, and more generic tasks into the CI_Controller class.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0
    hi dude

    It's look very nice

    But the code are too long its very bore


    Try to post codes very shortly dude

    Comments on this post

    • Strider64 disagrees : This is very short code compared to others I've seen. ;)
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    I tend to prefer a controller file (and usually class) per operation, but how you have it seems to be the norm of the Zend framework and probably others, so I'm probably in the minority.

IMN logo majestic logo threadwatch logo seochat tools logo