Welcome Guest, Not a member yet? Register   Sign In
Model (verify username & password
#1

[eluser]terry101[/eluser]
Hi i am currently working on building a login and i am verifying if the password and username are correct but not sure if i did my model correctly. I'm also confused on how my controller should respond to this can some one give me a hand. Thanks!!!

Code:
function check_login ($username, $password) {
        $en_password = MD5($password);
        
        $checklogin = array('username' => $username, 'password' => $en_password);
        $verifylogin = $this->db->where($checklogin);
    
        
        if ($verifylogin->num_row()== 1) {
            return true;
        }
        else {
            return false;
        }
    }
}

Here what i have on my controller as of now but confused on how to send model to controller with a true or false statement.

Code:
function login()    {
        
                $this->load->library('form_validation');
        
        $this->form_validation->set_rules('username', 'Username','required|xss_clean|trim');
        $this->form_validation->set_rules('password', 'Password','required|xss_clean|trim');
        
        
        
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('loginview');
        }
        else {
                echo ' YOUR IN!!!';
        }
    
    }
#2

[eluser]psychoder[/eluser]
Code:
function login()    {
        
                $this->load->library('form_validation');
        
        $this->form_validation->set_rules('username', 'Username','required|xss_clean|trim');
        $this->form_validation->set_rules('password', 'Password','required|xss_clean|trim');
        
        
        
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('loginview');
        }
        else {
                $this->load->model("model_name");
                if($this->model_name->check_login($username,$password))
                echo ' YOUR IN!!!';
                else
                echo 'invalid combination';

        }
    
    }
#3

[eluser]terry101[/eluser]
I'm confused so in the if statement it sends it to the function and if the function returns false how is it that i echo invalid combination for the else statement. How does it know which is false and which is true. what if the "if statement" is invalid combination and the "else statement is your in.

Code:
else {
                $this->load->model("model_name");
                if($this->model_name->check_login($username,$password))
                echo ' YOUR IN!!!';
                else
                echo 'invalid combination';

        }
    
    }
#4

[eluser]terry101[/eluser]
wait i see it, if the "if statement is true" echo your in.

Cool thank you sir. quick question is this the proper method of verifying login? i seen some other methods where the variable(username/password) were sent back to the controller and if it match they are logged in. What do you recommend?
#5

[eluser]CodeIgniteMe[/eluser]
[quote author="terry101" date="1312746486"]Hi i am currently working on building a login and i am verifying if the password and username are correct but not sure if i did my model correctly. I'm also confused on how my controller should respond to this can some one give me a hand. Thanks!!!

Code:
function check_login ($username, $password) {
        $en_password = MD5($password);
        
        $checklogin = array('username' => $username, 'password' => $en_password);
        $verifylogin = $this->db->where($checklogin);
    
        
        if ($verifylogin->num_row()== 1) {
            return true;
        }
        else {
            return false;
        }
    }
}
[/quote]

You forgot something there:
Quote: function check_login ($username, $password) {
$en_password = MD5($password);

$checklogin = array('username' => $username, 'password' => $en_password);
$verifylogin = $this->db->where($checklogin)->get('user_table'); // you should set the source table and then execute the query


if ($verifylogin->num_row()== 1) {
return true;
}
else {
return false;
}
}
}

If you don't do "get()" or something else similar in codeigniter, you can't execute the query.
#6

[eluser]CodeIgniteMe[/eluser]
It's better to use model to check for any data validation and manipulation. Controller is just the "Mayor" of your application, and the libraries and models are the workers. The views are only for presentation or "representatives".
#7

[eluser]psychoder[/eluser]
+1 to CodeIgniteMe...

You don't need to return the username and password to the controller. The data were passed from the controller to the model function, so the data can be displayed or manipulated in the controller without returning it from the model...
#8

[eluser]terry101[/eluser]
thats fine we can work on this together. did it work on your end? with the codes above?

i'm confused tho whats is the color=green supposed to represent? username=$username?

Code:
$verifylogin = $this->db->where($checklogin)[color=green]->get('user_table')[/color]; // you should set the source table and then execute the query

i passed what i had [above] and i got a error on line 11 in my model

line 11 in my model
Code:
if ($verifylogin->num_row()== 1) {

my whole model
Code:
<?php

class Loginmodel extends CI_Model {
    
    function check_login ($username, $password) {
        $en_password = MD5($password);
        
        $checklogin = array('username' => $username, 'password' => $en_password);
        $verifylogin = $this->db->where($checklogin);
    
        if ($verifylogin->num_row()== 1) {
            
            return TRUE;
        }
        else {
            
            return FALSE;
        }
    }
}
#9

[eluser]psychoder[/eluser]
Code:
class Loginmodel extends CI_Model {
    
    function check_login ($username, $password) {
        $en_password = MD5($password);
        
        $checklogin = array('username' => $username, 'password' => $en_password);
        $verifylogin = $this->db->where($checklogin)->get();
    
        if ($verifylogin->num_row()== 1) {
            
            return TRUE;
        }
        else {
            
            return FALSE;
        }
    }
}

->get where added to get the recordes after you filter it..
i haven't try your code.. Smile
#10

[eluser]terry101[/eluser]
ok i just tried that and i got a error

A Database Error Occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `username` IS NULL AND `password` = 'd41d8cd98f00b204e9800998ecf8427e'' at line 2

SELECT * WHERE `username` IS NULL AND `password` = 'd41d8cd98f00b204e9800998ecf8427e'

Filename: C:\wamp\www\logintest\system\database\DB_driver.php

Line Number: 330

i seen this on the user guide

Code:
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

what does the $limit and $offset variables represent?




Theme © iAndrew 2016 - Forum software by © MyBB