Message: Undefined property: CI_DB_mysqli_result::$level - php

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
}
}
?>

Related

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');

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

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;

Undefined property: Index::$session

How Can I Solve the issue When I am running this code I am getting the following error I have already loaded session library still i am getting this error
This is my controller
public function __construct(){
parent::__construct();
}
public function index() {
$this->load->database();
$this->load->helper('form', 'url');
$this->load->library('session');
}
public function process() {
$this->load->model('login_model');
$result = $this->login_model->validate();
if (!$result) {
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->load->view(base_url().'index',$msg);
} else {
if ($_SESSION['utype'] == 2 || $_SESSION['utype'] == 1) {
redirect(base_url() . 'admin/dashboard');
}
if ($_SESSION['utype'] == 3 && !empty($_POST['urlValue'])) {
redirect($_POST['urlValue']);
}
if (empty($_POST['urlValue'])) {
redirect('/');
}
}
}
This is my model
public function validate() {
$username = $this->security->xss_clean($this->input->post('email'));
$password = $this->security->xss_clean($this->input->post('password'));
$pwd = base64_encode($password);
$this->db->where('email', $username);
$this->db->where('password', $pwd);
$query = $this->db->get('adduser');
$row = $query->row();
if (count($row) > 0) {
$row = $query->row();
$data = array(
'uid' => $row->id,
'uname' => $row->firstname,
'utype' => $row->usertype,
'uemail' => $row->email,
'validated' => true
);
$this->session->set_userdata($data);
return $row;
}
return $row;
}
I am getting this problem:
Severity: Notice
Message: Undefined property: Index::$session
Filename: core/Model.php
Line Number: 77
Backtrace:
File: D:\xampp\htdocs\savepaise\application\models\Login_model.php
Line: 42
Function: __get
File: D:\xampp\htdocs\savepaise\application\controllers\Index.php
Line: 31
Function: validate
File: D:\xampp\htdocs\savepaise\index.php
Line: 315
Function: require_once
Fatal error: Call to a member function set_userdata() on a non-object in
D:\xampp\htdocs\savepaise\application\models\Login_model.php on line 42
A PHP Error was encountered
Severity: Error
Message: Call to a member function set_userdata() on a non-object
Filename: models/Login_model.php
Line Number: 42
I think you need to load all library inside the controller constructor function not only in index function
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->helper('form', 'url');
$this->load->library('session');
}
Update 1 : you can use autoload like this
Go to applications/config/autoload.php and in there you can edit what you need.
They are in arrays and seperated by packages, libraries, helpers, config, languages and models.
Example
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url', 'html', 'form');

Store model function return to controller function

I have a model which returns a username of the person that has logged into the website to a controller. I am trying to save the username into a variable which i can user to then insert back into another table, however i am having no luck saving the data. Below is my model and controller classes.
Model:
function is_loggedin()
{
$session_id = $this->session->userdata('session_id');
$res = $this->db->get_where('logins',array('session_id' => $session_id));
if ($res->num_rows() == 1) {
$row = $res->row_array();
return $row['name'];
}
else {
return false;
}
}
Part of my Controller:
public function index()
{
$loggedin = $this->authlib->is_loggedin();
if ($loggedin === false)
$this->load->view('login_view',array('errmsg' => ''));
else
{
$this->load->view('postquestion_view',array('username' => $loggedin));
$user = $loggedin['username'];
}
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $user;
Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Filename: controllers/postq.php
Line Number: 47
Here the error message is very clear. The variable $user in the last line of the function -action- askquestion() snippet is not defined. Basically, you have to read more about variables scope.
In your current situation, the code of index action should be in constructor and the variable user should be an object property. i.e it should defined globally in your controller's class and then takes its value from the constructor something like the following general demo:
<?php
class Blog extends CI_Controller {
public $user = false;
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $this->user; //NOTICE THIS LINE
}
?>

get an php fatal error in codeigniter controller

Here it is my code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database(); /* This function it's used to connect to database */
$this->load->model('User','user'); /* This call the model to retrieve data from db */
}
public function index()
{
if(!file_exists('application/views/_login.php'))
{
show_404();
}
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<h4 style="text-align:center;">','</h4>');
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','password','trim|required|xss_clean|callback_pass_check');
if($this->form_validation->run() == FALSE)
{
$data['title'] = "User Access";
$data['author'] = "Salvatore Mazzarino";
$this->load->view('templates/_header',$data);
$this->load->view('_login',$data);
$this->load->view('templates/_footer',$data);
}
else
{
redirect('home', 'refresh');
}
}
public function pass_check($pass)
{
$result = $this->user->find_user($this->input->post('username'),$pass);
if($result == 1)
{
$session_array = array();
foreach ($result as $row)
{
$session_array = array('id'=> $row->id,'username'=> $row->username); /* Create a session passing user data */
$this->session->set_userdata('logged_in',$session_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('pass_check',"Invalid username or password!</br>Try again, please!");
return FALSE;
}
}
}
Here it is the line
$this->session->set_userdata('logged_in',$session_array);
that cause the error:
PHP Fatal error: Call to a member function set_userdata() on a non-object
Someone could say me the reason?
Make sure the session library is loaded.
You can add it in application/config/autoload.php to be loaded automatically.
http://ellislab.com/codeigniter/user_guide/libraries/sessions.html

Categories