Undefined property error when using model - php

I'm a newbie using Codeigniter,
I have a login controller and a login model in CI, why do I get this error?
"Undefined property : Login::$Login_model"
Error in Line 44 in "login" Controller:
if ($this->Login_model->check_user($username, $password) == TRUE)
This is my "login" Controller.
<?php
class Login extends CI_Controller {
public function __login()
{
parent::__construct();
$this->load->model('Login_model', '', TRUE);
}
function index()
{
if ($this->session->userdata('login') == TRUE)
{
redirect('home');
}
else
{
$this->load->view('login/login_view');
}
}
function process_login()
{
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == TRUE)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->Login_model->check_user($username, $password) == TRUE)
{
//$datalevel = $this->Login_model->check_user($username);
$data = array('username' => $username, 'login' => TRUE);
$this->session->set_userdata($data);
redirect('home');
}
else
{
$this->session->set_flashdata('message', 'Username dan/atau password Anda salah');
redirect('login/index');
}
}
else
{
$this->load->view('login/login_view');
}
}
function process_logout()
{
$this->session->sess_destroy();
redirect('login', 'refresh');
}
And This is my "login_model" model
<?php
class Login_model extends CI_Model {
function Login_model()
{
parent::__construct();
}
var $table = 'user';
function check_user($username, $password)
{
$query = $this->db->get_where($this->table, array('username' => $username, 'password' => $password), 1, 0);
if ($query->num_rows() > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
}

It says that your Model is not loaded. You can fix this by changing name of below method
public function __login()
to
public function __construct()
This way it will be automatically called when your class is called and your Model will be automatically loaded.

Related

admin type on codeigniter

I'm having trouble of my admin type
in my db I have a type field which has 1 and 2 value
1 is for admin
2 is for cashier
now here's my controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
//functions
function login()
{
//http://localhost/tutorial/codeigniter/main/login
$data['title'] = 'Lending System Login';
$this->load->view("login", $data);
}
function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run())
{
//true
$username = $this->input->post('username');
$password = $this->input->post('password');
//model function
$user = $this->load->model('main_model');
if($this->main_model->can_login($username, $password))
{
$session_data = array(
'username' => $username
);
$this->session->set_userdata($session_data);
redirect(base_url() . 'main/enter');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url() . 'main/login');
}
}
else
{
//false
$this->login();
}
}
function enter(){
if($this->session->userdata('username') != '')
{
//check what type
//1 - admin
//2 - cashier
if($this->session->userdata('type') == 1)
{
// echo '<h2>Welcome - '.$this->session->userdata('username').'</h2>';
// echo '<label>Logout</label>';
redirect(base_url(). 'main/admin');
}
else
{
redirect(base_url(). 'main/cashier');
}
}
else
{
redirect(base_url() . 'main/login');
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect(base_url() . 'main/login');
}
}
now here is my model can_login
<?php
class Main_model extends CI_Model
{
function can_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
}
?>
my problem is that I couldn't get the admin type because i didn't type it it's already in the database and my problem is that how can I have an admin type
Please can someone please help me with this . I'm new to codeigniter
thank you.
If you have a column called user_type to define user role, Follow this steps
1) In your model, modify this
if($query->num_rows() > 0)
{
return $query->row_array(); // Send data from model to controller
}
else
{
return false;
}
2) In Controller, modify this
$user_data = $this->main_model->can_login($username, $password)
if($user_data)
{
$session_data = array(
'username' => $user_data['username'],
'type' => $user_data['user_type'],
);
$this->session->set_userdata($session_data);
redirect(base_url() . 'main/enter');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url() . 'main/login');
}
got it.
what i did was this
$session_data = array(
'username' => $username,
'type' => $type
);
$this->session->set_userdata($session_data);
if($this->session->userdata('username') != '')
{
if($this->session->userdata('type') == '1')
{
redirect(base_url(). 'main/admin');
}
else
{
redirect(base_url(). 'main/cashier');
}
}

User login status activation check in codeigniter?

I'm new in Codeigniter. I have a login system where user with status is 0 can't do login yet and where user with status 1 can login. I might be have a mistake in my code. So, i hope you can find where is my mistake and help me to make it right. here's my code.
My Controller
public function login() {
$this->form_validation->set_rules('no', 'No', 'required|min_length[10]|max_length[16]|integer');
$this->form_validation->set_rules('password', 'password', 'required|md5|xss_clean');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if($this->form_validation->run()== FALSE) {
$this->load->view('v_login');
}else{
$no = $this->input->post('no');
$password = $this->input->post('password');
$cek = $this->m_user->ambilPengguna($no, $password);
$status = $this->m_user->ambilStatus($no); //HERE'S THE PROBLEM
if($cek->num_rows()<> 0 && $status == '1') { //HERE'S TOO, IT WON'T CHECK THE STATUS.
$this->session->set_userdata('isLogin', TRUE);
$this->session->set_userdata('data_user',$cek->row());
redirect('c_belajar');
}else {
echo " <script>
alert('Login failed! call the administrator to activate your account');
history.go(-1);
</script>";
}
}
}
My Model
public function ambilPengguna($no, $password) {
$this->db->select('*');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$this->db->where('password', $password);
$query = $this->db->get();
return $query;
}
public function ambilStatus($no){
$this->db->select('status');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$query = $this->db->get();
return $query;
}
There's a mistake on controller. Please help me.
Alright! May I suggest a little code restructure
Controller
class YourController extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login_model');
$this->load->library('form_validation');
}
public function login()
{
if($_POST)
{
$config=array(
array(
'field' => 'no',
'label' => 'Number',
'rules' => 'trim|required',
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required',
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run()==false)
{
$data['errors']=validation_errors();
$this->load->view('login',$data);
}
else
{
$check=$this->login_model->checkUser($_POST); // you can use xss clean here filter post data
if(!$check)
{
$data['errors']='Invalid Password';
$this->load->view('login',$data);
}
elseif($check==1)
{
$data['errors']='Your account status is not active yet, Please contact Administrator';
$this->load->view('login',$data);
}
else
{
$this->session->set_uerdata($check);
redirect(base_url().'dashboard');
}
}
}
else
{
$this->load->view('login');
}
}
}
Model
class login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function checkUser($data)
{
$st=$this->db->select('*')
->from('tbl_user')
->Where('no_id', $data['no'])
->where('password', $data['password'])
->get()->result_array();// you can use row()
if(count($st)>0)
{
if($st[0]['status']==0){
return 1;
}
else
{
return $st[0];
}
}
else
{
return false;
}
}
}
Try this, this working in my project..
Your Model
public function ambilStatus(){
$this->db->where('no_id', $this->input->post('your input name'));
$query = $this->db->get($this->db->dbprefix . 'tb_user');
$ret = $query->row();
return $ret->account_status;
}
And Your Controller
$status = $this->m_user->ambilStatus();
if($status && $cek->num_rows() == 1 ) {

Unable to do a login redirect in codeigniter

I am trying to develop a login panel using codeigniter but I am unable to do so as I believe my concept is not so clear yet though or Am i doing something wrong please help me out with this concern
Controllers>admin.php
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run()) {
$this->verify_user->can_log_in();
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_id') == true) {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
models>verify_users.php
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
$this->load->view('admin/login', $data);
}
}
}
The thing is happening when I login with correct username and password it redirects me back to login.php when I put the model script within the verify function it runs perfectly
Please help me out with this
This is the closest possible fix to your way of implementation.
You need to consider reading more about MVC.
Try replace your controller with this:
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run() && $this->verify_user->can_log_in()) {
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_in') == "1") {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
}
And your model with this:
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => "1"
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
return false;
}
}
}
Check this
class admin extends CI_Controller {
^// this should be Admin
IN model
else {
//$data['message'] = 'Incorrect username/password';
//$this->load->view('admin/login', $data);
//dont load view in model
return false;
}
In controller
if($this->form_validation->run()) {
$res = $this->verify_user->can_log_in();
if($res)
redirect('admin/dashboard');
else
redirect('admin/login');
} else {
$this->load->view('admin/login');
}
Fixing these 3 errors should help you.

Codeigniter Login Authentication

I need help in the validations of my login page and authentication of the login page also.
Controller:
class User_authentication extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('cookie');
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->database();
}
function index() {
$this->session->set_userdata('message', '');
}
function loadLogin() {
$this->load->view('index.php');
}
function authenticateUser() {
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('pass', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_message('required', 'Invalid Password.');
if($this->form_validation->run() == FALSE) {
$this->load->view('index.php');
}
else {
redirect('homepage/loadhomeView');
}
}
function username_check($str) {
$username = $this->input->get_post('username');
if($str == '' || $str != $username) {
$this->form_validation->set_message('username_check', 'The username is invalid');
return FALSE;
}
else {
return TRUE;
}
}
function logout() {
$this->session->sess_destroy();
$this->load->view('index.php');
}
}
and Model:
class User_model extends CI_Model {
function login_user() {
$this->db->select('*');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->query->get();
if($query->num_rows() == 1) {
return $query->result();
}
else {
return false;
}
}
}
kindly guide me in doing this.. thank you

Codeigniter user access level

Model:
Class User extends CI_Controller
{
function login($username, $password)
{
$this->db->select('id', 'username', 'password', 'access');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}
Controller:
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user','',TRUE);
}
function index()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('home', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => #$row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
How to deal with access? In the database is int number, you need to make a selection from the base, to get the numbers from the table and access to work with him. How to implement it?

Categories