Welcome Guest, Not a member yet? Register   Sign In
User Login Validation - Please Help Me. Please Please Please....
#1

[eluser]dark_lord[/eluser]
Hi CodeIgniter People.

Im in need of an assistance here... I am a newbie in using code igniter and I just want to create a typical login script. Where user gets to log their username and password and redirect them from their account and if not successfully validated with the database the user will be redirected to the login page with an error message. hehe.. Hope you can help me also add sessions in it. Thank you so much peeps. have a great day.

Here are my start-up codes that I am working on right now:

View
Filename:login.php

Code:
html>
    <head><title><?=$title;?></title>
    </head>
<body>
                        <?
                        //Form Data
                        $username = array(
                'name'        => 'fvUsername',
                                'value'       => $this->validation->fvUsername,
                'maxlength'   => '20',
                'size'        => '20',
                );

            $password = array(
                'name'        => 'fvPassword',
                                'value'       => $this->validation->fvPassword,
                'type'          => 'password',
                'maxlength'   => '20',
                'size'        => '20',
                );
                        $clear = array(
                'name'        => 'fvClear',
                'value'       => 'Clear',
                'type'          => 'reset',
                );
                        ?>
     <table border="1" cellspacing="2" cellpadding="3" align="center" width="700" />
        <tr>
        <td>
        <table border="1" cellspacing="2" cellpadding="3" align="center" width="700" />
            <tr>
            <td>
            &lt;?=$this->validation->error_string; ?&gt;
            &lt;?php
            //$this->load->helper('form');
            echo form_open(); ?&gt;
            <table border="1" cellspacing="2" cellpadding="3" align="center" width="300" />
                <tr>
                <td>Username:</td>
                <td>&lt;? echo form_input($username); ?&gt;</td>
                </tr>
                <tr>
                <td>Password:</td>
                <td>&lt;? echo form_input($password); ?&gt;</td>
                </tr>
                <tr>
                <td>&nbsp;</td>
                <td>&lt;? echo form_submit('fvSubmit', 'Login'); echo form_input($clear); ?&gt;</td>
                </tr>
            </table>
            </td>
            </tr>
        </table>
        </td>
        </tr>
    </table>
&lt;/body&gt;
&lt;/html&gt;

----------------

MODEL
Filename:my_model.php

Code:
&lt;?php

    class My_model extends Model
    {

        function __Construct()
        {
            parent::Model();
        }
        
        function get_users($lastname)
        {
            $this->load->database();
            $this->db->select('lastname');
            $this->db->from('users');
            $query = $this->db->get();
            return $query->row();
        }
    
        function get_last_ten_entries()
        {
            $this->load->database();
            $query = $this->db->get('users');
            return $query->result();
        }
        
        function check_login_data($username, $password)
        {    
            $query = $this->db->select('username, password');
            $query = $this->db->from('users');
            $query = $this->db->where('username', $username);
            $query = $this->db->where('password', $password);
            $query = $this->db->get();
            
            if($query->num_rows() > 0)
            { return $query->result(); }
            else
            { return false;}
        }
    }

?&gt;

------------------------------------

CONTROLLER
Filename: blog.php

Code:
?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

    class Blog extends Controller
    {
        function Home()
        {
            parent::Controller();
        }
        function index()
        {
            $title['title'] = "Login";
            
            $this->load->library('validation');
            $this->load->model('blog/my_model');
            
            $rules['fvUsername'] = 'trim|required|callback__check_logindata';
            $rules['fvPassword'] = 'trim|required';
            $this->validation->set_rules($rules);
            
            $fields['fvUsername'] = 'fvUsername';
            $fields['fvPassword'] = 'fvPassword';
            $this->validation->set_fields($fields);
            
            $success['success'] = "Welcome Back";
            
            if($this->validation->run() == FALSE)
            {
                //$template['content'] = $this->load->view('products/success', $success, true);
                $frm = $this->load->helper('form');
                $this->load->view('products/login', $frm);
            }
            else
            {
                redirect('Blog/Success', 'refresh');
            }
        }
        
        function _check_logindata($fvUsername)
        {
            $fvPassword = $this->validation->fvPassword;
            
            //$this->load->model('blog/my_model');
            if ($this->my_model->check_login_data($fvUsername, $fvPassword) == FALSE)
            {
                $this->validation->set_message('_check_logindata', 'Username and Password might be incorrect!');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }

                function Success()
        {
            $success['success'] = "Success";
            $this->load->view('products/success', $success);
        }
    }
?&gt;

Hope you can help me with this.

Thanks alot in advance!

Regards,

Wish Bear!


Messages In This Thread
User Login Validation - Please Help Me. Please Please Please.... - by El Forum - 11-13-2007, 10:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB