Welcome Guest, Not a member yet? Register   Sign In
Login Form Help
#1

[eluser]tdktank59[/eluser]
So I've just started using Code Igniter recently and I figured I'd start with a simple login form with validation.

Yes I know there is backend pro and what not, but I'm teaching myself by doing.

So basicaly ive gotten everything from the user guide

authentication.php
Code:
<?php

class Authentication extends Controller {

    function Authentication()
    {
        parent::Controller();
    }

    function index()
    {
        /*$this->session->set_userdata('login',FALSE);
        $data = array( 'login' => $this->session->userdata('login') );
        $this->load->view('authentication_message',$data);*/

        if ($this->session->userdata('login') == TRUE)
        {
            redirect('welcome','refresh');
        }
        else
        {
            $this->login();
        }
    }

    function login()
    {        
        $this->load->library('validation');

        $rules['username']    = "trim|required|min_length[5]|max_length[25]|callback_username_check";
        $rules['password']    = "trim|required|min_length[5]|max_length[25]|md5";
        
        $this->validation->set_rules($rules);
        
        $fields['username']    = 'Username';
        $fields['password']    = 'Password';
    
        $this->validation->set_fields($fields);
    
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        
        if ($this->validation->run() == FALSE)
        {
            $data = array ( 'param' =>     array(    'username'    =>    array(    'name'      => 'username',
                                                                        'id'        => 'username',
                                                                        'value'     => $this->validation->username,
                                                                        'maxlength' => '25',
                                                                        'size'      => '20'),
                                                                        
                                                  'password'    =>    array(    'name'         => 'password',
                                                                          'id'        => 'password',
                                                                          'value'        => $this->validation->password,
                                                                          'maxlength' => '25',
                                                                          'size'        => '20'),
                                                                          
                                                'submit'    =>    array(    'name'        => 'submit',
                                                                        'id'        => 'submit',
                                                                        'value'        => 'Submit')
                                                ));
                                                                        
            $this->load->view('login_form',$data);
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }

    function username_check($username)
    {
        if ($username == 'username')
        {
            return TRUE;
        }
        else
        {
            $this->validation->set_message('username_check', 'Invalid username used');
            
            return FALSE;
        }
    }
}

login_form.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;

&lt;style type="text/css"&gt;
body {
    font-family: monospace;
    font-size: 12px;
}
ul {
    padding: 0px;
    margin: 0px;
    list-style: none;
}
ul li {
    padding: 2px;
}
#form {
    width: 300px;
    margin: auto auto auto auto;
    position: relative;
}
fieldset {
    border: 1px solid #CCCCCC;
}
legend {
    font-weight: bold;
    font-size: 16px;
}
form {
    margin: 0px;
    padding: 0px;
}
.login label {
    font-weight: bold;
}
.error {
    width: 300px;
    margin: auto auto auto auto;
    position: relative;
    background-color: #FFFED0;
    border: 1px dotted #FFA29B;
    padding: 5px;
    text-align: center;
    margin-bottom: 5px;
}

&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo $this->validation->error_string; ?&gt;

<div id="form">
    <fieldset>
        <legend>Login</legend>
        &lt;?php echo form_open('authentication/login'); ?&gt;
        <ul class="login">
            <li><label for="username">Username:&lt;?php echo form_input($param['username']); ?&gt;</label></li>
            <li><label for="password">Password:&lt;?php echo form_password($param['password']); ?&gt;</label></li>
            <li>&lt;?php echo form_submit($param['submit']); ?&gt;</li>
        </ul>
        &lt;?php echo form_close(); ?&gt;
    </fieldset>
</div>


&lt;/body&gt;
&lt;/html&gt;

However for login I want to match the username with the password.
Using whats already included with code igniter how would I do this?

Note I would be using a database
Table: user
Fields: username, password
#2

[eluser]Sumon[/eluser]
change your username_check() function code as
Code:
function username_check($username)
{
    $this->load->model("member_model");
    $password = $this->validation->password;
    if ($this->member_model->MemberLogin($MemEmail, $password) == FALSE)
    {
        $this->validation->set_message('username_check', 'Invalid username used');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
In addition use model to check username and password matching. Here is a sample one you may use
Model named application/models/member_model.php
Code:
function MemberLogin($username, $password)
{
    $query = $this->db->query("SELECT * FROM user WHERE username='$username' AND password='$password'");
    if ($query->num_rows() > 0)
        return TRUE;
    else
        return FALSE;
}
#3

[eluser]tdktank59[/eluser]
Looks good, Ill give it a test later on but the syntax checks out and should work.


Thanks for the helping hand




Theme © iAndrew 2016 - Forum software by © MyBB