[eluser]bhakti.thakkar[/eluser]
Dear all,
I am a new bie to CI. can some one help me with complete code of validating a username script. if username present in the DB then make entry in the LoginHistory_T table and redirect him to the main.php and if not give him a error message.
My three files (MVC) are:
mlogin.php
login_view.php
login.php
mlogin.php code:
Code:
<?php
class Mlogin extends Model
{
function __Construct()
{
parent::Model();
}
function get_users($username)
{
$this->load->database();
$this->db->select('Username_VC');
$this->db->from('Users_T');
$query = $this->db->get();
return $query->row();
}
function get_last_ten_entries()
{
$this->load->database();
$query = $this->db->get('Users_T');
return $query->result();
// print $query->num_rows();
}
function check_login_data($username, $password)
{
$query = $this->db->select('Username_VC, Password_VC');
$query = $this->db->from('Users_T');
$query = $this->db->where('Username_VC', $username);
// $query = $this->db->where('Password_VC', $password);
$query = $this->db->get();
if($query->num_rows() > 0)
{ return $query->result(); }
else
{ return false;}
}
}
function addUsersHistory(){
$now = date("Y-m-d H:i:s");
$data = array(
'Username' => $this->input->xss_clean($this->input->post('username')),
'RemoteAddr_VC' => $this->input->ip_address(),
'LoginDate_DT' => $now
);
$this->db->insert('LoginHistory_t', $data);
}
?>
code in login.php
Code:
<?php
class Login extends Controller {
function Login()
{
parent::Controller();
$this->load->helper('url');
}
function index()
{
$data['base'] = $this->config->item('base_url');
$data['css'] = $this->config->item('css');
$this->load->helper(array('form', 'url')); // load form
$this->load->library('validation'); // load validations
$rules['username'] = 'trim|required|max_length[12]|xss_clean|callback__check_logindata';
$rules['password'] = 'trim|required|md5';
// set values for repopulating fields
$fields['username']='Username';
$fields['password']= 'Password ';
$this->validation->set_fields($fields);
$this->validation->set_rules($rules);
if ($this->validation->run() == TRUE)
{
//$this->load->view('checkuser' , $data);
$this->load->model('Mlogin','',TRUE);
$password = $this->validation->password;
$username = $this->validation->username;
if ($this->Mlogin->check_login_data($username, $password) == FALSE)
{
$this->validation->set_message('_check_logindata', 'Username and Password might be incorrect!');
return FALSE;
}
// $this->Mlogin->addHistory();
// redirect('welcome/thankyou','refresh');
}
else
{
$this->load->view('login_view' , $data);
}
}
function _check_logindata($username)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('Mlogin');
if ($this->Mlogin->check_login_data($username, $password) == FALSE)
{
$this->validation->set_message('_check_logindata', 'Username and Password might be incorrect!');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
there is some mistake somewhere. i get the error message:
Severity: Notice
Message: Undefined property: Mlogin::$db
Filename: models/mlogin.php
Line Number: 31
my DB connection is fine. i printed the number of records in a table and it did print
can some one help me to rectify my code or help me with some sample script link
Million Thanks in advance