Welcome Guest, Not a member yet? Register   Sign In
User Login Page?
#1

[eluser]ywftdg[/eluser]
Hey guys, I have been prowling these boards all day, but nothing really seems to make sense to me. I am still new to CI, and a basic PHP coder, so bare with me.

I am trying to create a basic login page, I have about 20 users in my database. I am trying to rebuild this section using CI. I just don't understand how to make a MVC model for this, to help get my project started.

Do I need two controllers, one to load login form and another to check the login information sent? Can this be done with one controller? In the past my index would be login, if they login, the index loads the user front page content as well.

Then a model to query the database to see if the user exists, if so if the password is correct?

I am sure may of you guys have done this many times, maybe someone has some basic code for me to study? Any help would be greatly appreciated.
#2

[eluser]johnwbaxter[/eluser]
You can do it all in one controller no problem.

Have a look in the wiki http://codeigniter.com/wiki/Simplelogin an auth library called simplelogin. Download it and have a look at the sample controller and views in it. They should be very useful.
#3

[eluser]Nathan Moore[/eluser]
Simplelogin is a good library to reference.

If you wanted to build something yourself, you could use a custom validation method. Basically, you would check validation at the beginning of your login method in the controller.

Have the custom validation function check the database to verify the user credentials are correct. If so, allow them to login, set a cookie/session, etc.

If not, show the form.

You will also want to make sure the authentication checks out at the beginning of the controller for any password-protected area.

Check out this page: http://ellislab.com/codeigniter/user-gui...ation.html
And look under the "Callbacks: Your own Validation Functions" heading

I hope that helps!
#4

[eluser]Developer13[/eluser]
Your controller:

class login extends Controller {

function login() {

parent::Controller();

}

function index() {

$rules = array('username'=>'required','password'=>'required');
$this->validation->set_rules($rules);

$fields = array('username'=>'Username','password'=>'Password');
$this->validation->set_fields($fields);

if ($this->validation->run()) {

// the form has been submitted and has validated - let's process

// check to see if username and password authenticates against database

// if it does, redirect to where they need to be

// if not, redirect to login form again

}

else {

// the form has *not* been submitted and / or has not validated - let's display the form

$this->load->view('form');

}

}

}
#5

[eluser]ywftdg[/eluser]
Thanks guys, I will review all this today and see if I can get something going. I ran across that validation document when my eyes were on fire last night, and put that on my first thing to read this morning before continuing.

Call me crazy, but maybe this could be suggested for something to be a helper in future versions, like a basic user/pass login thing built into CI?
#6

[eluser]Colin Williams[/eluser]
Quote:Call me crazy, but maybe this could be suggested for something to be a helper in future versions, like a basic user/pass login thing built into CI?

Well, it is the most active area of development in the community, I believe. Check the wiki.

As far as being part of CI core, it won't happen. It doesn't fit the vision of EllisLab, which is to have a flexible, very portable application development framework. If it were to become part of core, they would be locking you into specific conventions and a specific data schema, to an extent. The only place CI does this currently is there Session Class, except that's only if you enable storing session info in the DB. I can't imagine a user system that works without a DB (besides simple username/password combos). It's best left in the realm of contributed solutions.
#7

[eluser]ywftdg[/eluser]
Ok, like I said, I am still new, i'm missing some logic in all this. I have been trying to tweak from the lesson in the user guide, but the part with checking the database is doing nothing. I think the values are not being sent to my query or something. I will later move my query to the models, but for right now just have it in the controller. Can anyone lend some advice on what is wrong with this?

Code:
class Form extends Controller {
    
    /////////////////////////////
    function index()    {
    
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        
        
        //Set rules for forms
        $rules['email']     = "trim|required|valid_email";
        $rules['password']    = "required";
        $this->validation->set_rules($rules);
        
    
        //Reset form keeps email entered.
        $fields['email'] = 'Email';
        $fields['password'] = 'Password';
        $this->validation->set_fields($fields);

    
        //check the status
        if ($this->validation->run() == TRUE)    {
            
             $this->db->select('*');
            $this->db->where('designer.dEmail' , '$email');
            $this->db->where('designer.dPassword' , '$password');
            $this->db->from('designer');
            $query = $this->db->get();
            
            if ($query->num_rows() > 0) {
                $this->load->view('formsuccess');
            } else    {
                $this->load->view('myform');
                }
        }
        
        else    {
            $this->load->view('myform');
        }
            
    }

    
    /////////////////////////////
    }


myform.php (view)
Code:
<? $attributes = array('class' => 'accountForm', 'id' => 'form') ?>

    &lt;?=$this->validation->set_error_delimiters('<div><strong>', '</strong></div>')?&gt;
    &lt;?=$this->validation->error_string?&gt;
    
    
    &lt;?=form_open (base_url().'form', $attributes) ?&gt;
    &lt;?=form_hidden ('go', 'true')?&gt;
    
    <div class="accountEntryA">
        <div class="accountLeft">Email</div>
        <div class="accountRight">&lt;?=form_input('email', $this->validation->email)?&gt;</div>
        <div class="clear"></div>
        
        <div class="accountLeft">Password</div>
        <div class="accountRight">&lt;?=form_password('password')?&gt;</div>
        <div class="clear"></div>
    </div>
    
    <div class="accountEntry">
        <div class="accountLeft">&lt;?=form_submit('submit', 'Submit')?&gt;</div>
        <div class="accountRight"></div>
        <div class="clear"></div>
    </div>
    
    &lt;?=form_close()?&gt;
#8

[eluser]ywftdg[/eluser]
Nevermind, got it. Found this post helpful: http://ellislab.com/forums/viewthread/57019/

I didn't know about 'input->post'...That sure helps things! hahah, learning!
#9

[eluser]Bramme[/eluser]
I've written a tutorial on this subject today. Meant to do it earlier, but seeing this thread today made me write it out today.

Auth library tutorial
#10

[eluser]ywftdg[/eluser]
Bramme, thanks for posting that, I am going to be trying this tomorrow now. Today I got it all working, but now with my sessions and such, hitting more walls. So going to follow your guide step by step and try to make my system work the same. Skimmed it now, but will try with a fresh brain tomorrow morning. Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB