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!
#2

[eluser]gtech[/eluser]
[url="http://www.michaelwales.com/2007/10/erkana-codeigniter-authorization-library/"]www.michaelwales.com[/url]

looks like someones done the hard work for you!
#3

[eluser]dark_lord[/eluser]
[quote author="gtech" date="1195058720"][url="http://www.michaelwales.com/2007/10/erkana-codeigniter-authorization-library/"]www.michaelwales.com[/url]

looks like someones done the hard work for you![/quote]


master, I have tried your sample blog that can be seen at your site? I think I have followed all the instructions... How come my login and register page won't work. Please help me.... it is just redirecting to the login page and the register page.

Hope you can help me with this.

Thank you in advance.

Regards,

wish bear!
#4

[eluser]gtech[/eluser]
Firstly I cant take credit for the site as its not my work in anyway shape or form!. Its just a nifty lightweight library you can download to help you do authentication with sessions and to save you writing the code yourself.

I would recommend putting some debug information in your controller and model code, so you can diagnose where the problem is (don't forget to take the echos out or you will get a headers error on the redirect). echo will display text to your browser, and print_r(<the array/object you want to display>) can used to print out the contents of arrays and objects.

for example:
Code:
if ($this->my_model->check_login_data($fvUsername, $fvPassword) == FALSE)
            {
                $this->validation->set_message('_check_logindata', 'Username and Password might be incorrect!');
echo "returning FALSE<br>";
                return FALSE;
            }
            else
            {
echo "returning TRUE<br>";
                return TRUE;
            }

displaying these messages can help you trace the code path, and will eventually lead you to the problem.

I use this method all the time during development and then delete the echo/print_r's when it works.. If I want to leave debug in then I use the [url="http://ellislab.com/codeigniter/user-guide/general/errors.html"](log_message)[/url] function, but stick with echos and print_rs for now.
good luck!
#5

[eluser]Michael Wales[/eluser]
@wish_bear:
The blog tutorial is incomplete at that site (as the article says, I can't do everything for you).

I believe gtech was pointing you to ErkanaAuth - which is a really simple authorization library.
#6

[eluser]gtech[/eluser]
[quote author="walesmd" date="1195075158"]@wish_bear:
The blog tutorial is incomplete at that site (as the article says, I can't do everything for you).

I believe gtech was pointing you to ErkanaAuth - which is a really simple authorization library.[/quote]

Apologies, yes I was pointing to your Erkana library, the link took you there but the website url I displayed was incomplete.
#7

[eluser]dark_lord[/eluser]
Wow! thats great! thanks! I will try to use those syntax in my development. Thank you ver much.

Hope you can help me again if there will come another question in my study of code igniter.

Thanks alot master.

Regards,

Wishbear.
#8

[eluser]dark_lord[/eluser]
Master,


Hi!, Can u help me create a authentication page? with a session integrated to it. I know there are alot of code out there that is free to be copied and use.

But what I want is something like for a beginner like me hehe.. something like a authentication script created from CI that I can comprehend as a beginner.

hope you help me with this master even I know you're kindda a busy person.. hehey.. ^_^

Hope you can provide me some code snippet of a authentication script.

Thank you very much master.

Regards,

Wishbear.




Theme © iAndrew 2016 - Forum software by © MyBB