TMessage: Call to a member function CheckUser() on null - php

controller
<?php
error_reporting(E_ALL ^ E_NOTICE);
class Login extends CI_Controller {
public function _construct()
{
parent::_construct();
$this->load->model('MUser');
}
public function index()
{
if ($this->session->userdata('logged') == true ) {
redirect('rental') ;
}else{
$this->load->view('login');
}
}
public function validasi()
{
$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');
if($this->MUser->CheckUser ($username,$password) == true) {
$data = array('username'=>$username, 'logged'=> true);
$this->session->set_userdata($data);
redirect('rental');
}else{
$this->session->set_flashdata('pesan', 'Username atau password anda salah');
redirect('Login');
}
} else {
$this->load->view('login');
}
}
public function logout()
{
$this->session->session_destroy();
redirect('Login', 'referesh');
}
}
?>
model
<?php
error_reporting(E_ALL ^ E_NOTICE);
class MUser extends CI_Model {
public $table = "user";
public function _construct()
{
parent::_construct();
}
public function CheckUser($username, $password) {
$query = $this->db->get_where($this->table, array('username'=>$username, 'password'=>$password));
if($query->num_rows() > 0)
{
return true;
} else {
return false;
}
}
}
?>
An uncaught Exception was encountered
Type: Error
Message: Call to a member function CheckUser() on null
Filename: C:\xampp\htdocs\rental\application\controllers\Login.php
Line Number: 30
Backtrace:
File: C:\xampp\htdocs\rental\index.php
Line: 315
Function: require_once
pliss answer my question

Try this method
"This wrong ini line 30"
public function CheckUser($username, $password) {
$query = $this->db->get_where($this->table,
array('username'=>$username, 'password'=>$password));
if($query->num_rows() > 0)
{
return true;
} else {
return false;

Related

Login Attempts - Codeigniter - Not Working

I want to workout a function such that unsuccessful login attempts of user are capped at 3 consecutive failed login attempts, then serve them a message to that effect. It is immediately executing this line:
if ($isBlocked) {
$this->form_validation->set_message('check_user', 'Account is temporarily blocked.');
}
Somethings wrong on my code. Thanks in advance for the help.
Controller
<?php
class Account_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['title'] = 'Account Login';
$this->load->view('account_login', $data);
}
public function verify()
{
$this->form_validation->set_rules('acc_username', 'Username', 'required');
$this->form_validation->set_rules('acc_password', 'Password', 'required|callback_check_user');
if ($this->form_validation->run() === TRUE) {
echo 'Success';
} else {
$this->index();
}
}
public function check_user()
{
$username = $this->input->post('acc_username');
$password = $this->input->post('acc_password');
$this->load->model('account_login_model');
$login = $this->account_login_model->login($username, $password);
if ($login) {
return true;
} else {
if (isset($_SESSION['error_count'][$username])) {
$_SESSION['error_count'][$username] += 1;
} else {
$_SESSION['error_count'][$username] = 1;
}
$isBlocked = $this->account_login_model->isBlocked($username);
if ($isBlocked) {
$this->form_validation->set_message('check_user', 'Account is temporarily blocked.');
} else if (isset($_SESSION['error_count'][$username]) && $_SESSION['error_count'][$username] > 2) {
$this->account_login_model->block($username);
$this->form_validation->set_message('check_user', '3 consecutive failed login attempts. Account Blocked.');
} else {
$this->form_validation->set_message('check_user', 'Invalid Username/Password');
}
return false;
}
}
}
Model
<?php
class account_login_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function login($username, $password)
{
$condition_array = array(
'acc_username' => $username,
'acc_password' => $password
);
$rs = $this->db->get_where('accounts', $condition_array);
return $rs->row_array() ?: false;
}
public function isBlocked($username)
{
$condition_array = array(
'acc_username' => $username,
'acc_isBlocked' => 1
);
$rs = $this->db->get_where('accounts', $condition_array);
$row_count = count($condition_array);
if ($row_count > 0) {
return true;
} else {
return FALSE;
}
}
public function block($username)
{
$this->load->library('email');
$email = $this->account_lookup($username, 'acc_email');
$this->email->from('<email>', 'Yahoo.com');
$this->email->to($email);
$this->email->subject('Account Blocked');
$message = $this->load->view('account_blocked', null, TRUE);
$this->email->message($message);
$this->email->send();
$this->db->where('acc_username', $username);
return $this->db->update('accounts', array('acc_isBlocked' => 1));
}
public function account_lookup($username, $return)
{
$rs = $this->db->get_where('account', array('acc_username' => $username));
$row = $rs->row();
return $row->$return;
}
}

CodeIgniter - Message: Undefined property: Account_login::$login & Call to a member function model() on a non-object

Wanted to seek a help on these errors.
controller name: Account_login.php
model name: account_login_model.php
Message: Undefined property: Account_login::$login
Filename: controllers/account_login.php
Line Number: 34
Backtrace:
File: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line: 34
Function: _error_handler
File: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line: 21
Function: run
File: C:\xampp\htdocs\labexercise009\index.php
Line: 315
Function: require_once
Call to a member function model() on a non-object
Message: Call to a member function model() on null
Filename: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line Number: 34
Backtrace:
File: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line: 21
Function: run
File: C:\xampp\htdocs\labexercise009\index.php
Line: 315
Function: require_once
Here's my code:
Controller
?php
class Account_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['title'] = 'Account Login';
$this->load->view('account_login', $data);
}
public function verify()
{
$this->form_validation->set_rules('txtuser', 'Username', 'required');
$this->form_validation->set_rules('txtpass', 'Password', 'required|callback_check_user');
if ($this->form_validation->run() == TRUE) {
echo 'Success';
} else {
$this->index();
}
}
public function check_user()
{
$username = $this->input->post('txtuser');
$password = $this->input->post('txtpass');
$this->login->model('account_login_model');
$login = $this->account_login_model->login($username, $password);
if ($login) {
return true;
} else {
if (isset($_SESSION['error_count'][$username])) {
$_SESSION['error_count'][$username] += 1;
} else {
$_SESSION['error_count'][$username] = 1;
}
$isBlocked = $this->account_login_model->isBlocked($username);
if ($isBlocked) {
$this->form_validation->set_message('check_user', 'Account is temporarily blocked.');
} else if (isset($_SESSION['error_count'][$username]) && $_SESSION['error_count'][$username] > 2) {
$this->account_login_model->block($username);
$this->form_validation->set_message('check_user', '3 consecutive failed login attempts. Account Blocked.');
} else {
$this->form_validation->set_message('check_user', 'Invalid Username/Password');
}
return false;
}
}
}
Model
<?php
class Account_login_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function login($username, $password)
{
$condition_array = array(
'user_name' => $username,
'user_pass' => $password
);
$rs = $this->db->get_where('users', $condition_array);
$row_count = count($rs->row_array());
if ($row_count > 0) {
return $rs->row_array();
} else {
return FALSE;
}
}
public function isBlocked($username)
{
$condition_array = array(
'user_name' => $username,
'acc_isBlocked' => 1
);
$rs = $this->db->get_where('accounts', $condition_array);
$row_count = count($condition_array);
if ($row_count > 0) {
return true;
} else {
return FALSE;
}
}
public function block($username)
{
$this->load->library('email');
$email = $this->account_lookup($username, 'acc_email');
$this->email->from('lslayugan#feutech.edu.ph', 'Your Website');
$this->email->to($email);
$this->email->subject('Account Blocked');
$message = $this->load->view('account_blocked', null, TRUE);
$this->email->message($message);
$this->email->send();
$this->db->where('acc_username', $username);
return $this->db->update('accounts', array('acc_isBlocked' => 1));
}
public function account_lookup($username, $return)
{
$rs = $this->db->get_where('account', array('acc_username' => $username));
$row = $rs->row();
return $row->$return;
}
}
might be change like this
$this->load->model('account_login_model');
instead of
$this->login->model('account_login_model');

if else statement (not properly executing) - codeigniter

I have a function index() in my Admin.php controller and I'm trying to execute the 'if' statement but it is executing the 'else' statement. The admin user and password that I am logging in is correct. There's something wrong in my code and could anyone here please help me. Thanks in advance. :)
Here's my code:
//Admin.php controller
<?php
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
if ($this->session->userdata('logged_in') !== TRUE) {
redirect('Login');
}
}
function index()
{
if ($this->session->userdata('level') === '1') {
$this->load->view('admin_view');
} else {
echo "Access Denied";
}
}
}
//Login.php controller
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Login_model');
}
public function index()
{
$this->load->view('login_view');
}
public function auth()
{
$username = $this->input->post('user_name', TRUE);
$password = $this->input->post('user_pass', TRUE);
$result = $this->Login_model->check_user($username, $password);
if ($result->num_rows() > 0) {
$data = $result->row_array();
$name = $data['user_name'];
$level = $data['user_lvl'];
$sesdata = array(
'user_name' => $username,
'user_lvl' => $level,
'logged_in' => TRUE
);
$this->session->set_userdata($sesdata);
if ($level === '1') {
redirect('Admin');
} elseif ($level === '2') {
redirect('User');
}
} else {
echo "<script>alert('Access Denied');history.go(-1);</script>";
}
$this->load->view('login_view');
}
}
I Suggest use double equals instead triple equals
if ($this->session->userdata('level') == '1') {
$this->load->view('admin_view');
} else {
echo "Access Denied";
}
And also check if level is string or int..

Message: Undefined property: CI_DB_mysqli_result::$level

I have a difficulty on my program. The error say :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysqli_result::$level
Filename: controllers/Auth.php
Line Number: 30
Backtrace:
File:
C:\xampp\htdocs\PKLTelkom\Telkom2\application\controllers\Auth.php
Line: 30 Function: _error_handler
File: C:\xampp\htdocs\PKLTelkom\Telkom2\index.php Line: 315 Function:
require_once
Here's my controller named "Auth" :
<?php
/**
*
*/
class Auth extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('m_login');
}
public function index()
{
$this->load->view("login");
}
public function AksiLogin()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$passwordx = md5($password);
$login = $this->m_login->data_login($username, $passwordx);
$tes = count($login);
if ($tes > 0) {
//ambil detail data
$row = $this->m_login->data_login($username, $passwordx);
$level = $row->level;
//daftarkan session
$data_session = array('level' => $level);
$this->session->set_userdata($data_session);
//direct page
if ($level == 'superadmin') {
redirect('superadmin');
}
else if ($level == 'admin') {
redirect('admin');
}
}
else {
$this->index();
}
}
public function logout()
{
$this->session->unset_userdata("login");
$this->session->unset_userdata("username");
redirect ('index.php/auth');
}
}
?>
Here's my models named "M_Login" :
<?php
class M_login extends CI_Model
{
function data_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
return $this->db->get('akun');
}
}
?>
Looks like your return $this->db->get('akun'); on "M_login" model doesn't return object with the name level.
Try changing this line on "M_Login" model :
<?php
class M_login extends CI_Model
{
function data_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
return $this->db->get('akun')->row(); // change this line
}
}
?>

How can I pass variable between controller functions

This is my first time doing web programming. I want to make one variable that I can use on some functions, I use public $username; and public $password; and use $this->username and $this->password; but it didn't work. This is my code on controller;
public $can_log ;
public function home(){
$this->load->model("model_get");
$data["results"] = $can_log;
$this->load->view("content_home",$data);
}
public function login(){
$this->load->view("site_header");
$this->load->view("content_login");
$this->load->view("site_footer");
}
public function login_validation(){
$this->load->library('form_validation');
$this->load->view("site_header");
$this->load->view("site_nav");
$this->form_validation->set_rules('username','Username','required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|trim');// use md5 if want to encrpyt this
if($this->form_validation->run()){
redirect('site/home');
} else {
$this->load->view('content_login');
}
}
public function validate_credentials(){
$this->load->model('model_get');
$username = $this->input->post('username');//"user";
$password = $this->input->post('password');//"password";
//I tried both but none of those work
$this->can_log = $this->model_get->can_log_in($username, $password);
if($this->can_log){
return true;
} else {
$this->form_validation->set_message('validate_credentials','Incorrect username/password.');
return false;
}
}
I also tried with public $username and public $password but still can't get it
on my model;
public function can_log_in($username, $password){
$query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = '$username' and id_password = '$password'");
if($query->num_rows() > 0) {
$data = $query->result(); // fetches single row: $query->row();
return $data; //fetches single column: $data->col1;
}
}
so how can I get can_log - that contains col1 and col2 - to other function?
Maybe something like this?
public function with_parameter($parameter)
{
do something with $parameter
}
And then call the function
with_parameter($can_log);
I didn't understood the exact requirements, but try below code if it works for you.
Have followed some CI guidelines which you need to learn.
Controller:
class Controller_name extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("model_get"); // load models in constructor
$this->can_log = "some value"; // is the way to define a variable
}
public function home()
{
$data["results"] = $this->can_log; // is the way to retrieve value
$this->load->view("content_home",$data);
}
public function validate_credentials()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$is_valid = $this->model_get->can_log_in($username, $password);
if($is_valid)
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials','Incorrect username/password.');
return false;
}
}
}
Model:
class Model_get extends CI_Model
{
public function can_log_in($username, $password)
{
$where_aray = array("id_login" => $username, "id_password" => $password);
$query = $this->db->get_where("table", $where_array);
if($query->num_rows() > 0)
return $query->row();
return false;
}
}

Categories