Query Not Loading In Libaray File In CI - codeigniter

I am trying to convert my model user to a library file and place it in the system libraries. because I have multiple installs of codeigniter.
On my line 16 which is just below my public function login, its saying Call to a member function query() on a non-object in
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
private $user_id;
private $username;
public function __construct() {
$CI =& get_instance();
$CI->load->library('session');
$CI->load->library('bcrypt');
$CI->load->database();
}
public function login() {
// Line 16 $user_query = $this->db->query("SELECT * FROM " . $this->input->get('db_prefix') . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = '" . $this->db->escape($password) . "') AND status = '1'");
$result = $this->check_credentials($password);
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];
$user_query = $this->db->get('user');
if($user_query->num_rows == 1) {
return true;
} else {
return false;
}
}
public function check_credentials() {
$user_query = $this->db->get('user');
if ($user_query->num_rows == 1) {
$result = $query->row_array();
if ($this->bcrypt->check_password($password, $result['password'])) {
//We're good
return $result;
} else {
//Wrong password
return array();
}
} else {
return array();
}
}
public function isLogged() {
return $this->user_id;
}
public function getId() {
return $this->user_id;
}
public function getUserName() {
return $this->username;
}
}

After having a break and thinking about it I did re try with what #Mehul Jethloja said
And got it working
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->library('bcrypt');
$this->CI->load->database();
}
public function login($username, $password) {
$this->CI->db->where('username', $this->CI->input->post('username'), $username);
$this->CI->db->where('password', $this->CI->input->post('password'), $password);
$result = $this->check_credentials($password);
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];
$user_query = $this->CI->db->get('user');
if($user_query->num_rows == 1) {
return true;
} else {
return false;
}
}
public function check_credentials() {
$user_query = $this->CI->db->get('user');
if ($user_query->num_rows == 1) {
$result = $query->row_array();
if ($this->bcrypt->check_password($password, $result['password'])) {
//We're good
return $result;
} else {
//Wrong password
return array();
}
} else {
return array();
}
}
public function isLogged() {
return $this->user_id;
}
public function getId() {
return $this->user_id;
}
public function getUserName() {
return $this->username;
}
}

Related

How to differentiate the multiple panels with login and session?

It create the session but does not go to index2 and index3 always redirect with else and go to index method but i want to go index2 and index3 to handle other panels also.
Session is created successfully for all just comming else condition all the time.
My form data and array is also showing when i using the print_r for my code to view if the data is comming or not.
Problem is it is showing no any error just redirect with file of index method.
My Controller
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Main_Model');
$this->load->helper('url');
$this->load->library('session');
$method = $this->router->fetch_method();
$methods = array('index','index2','index3');
if(in_array($method,$methods))
{
if(!$this->session->has_userdata('signup_email'))
{
redirect(base_url('Main/login'));
}
}
}
public function index()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('BKO/index');
}
}
public function index2()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Admin/index');
}
}
public function index3()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Owner/index');
}
}
public function login()
{
//$data['select'] = $this->Main_Model->get_select();
$this->load->view('login');
}
public function login_process()
{
//$roll = $this->input->post('select');
echo $email = $this->input->post('email');
echo $pass = $this->input->post('upass');
$query = $this->Main_Model->login_process($email,$pass);
if($query == TRUE)
{
$this->session->set_userdata('signup_email');
$session = array(
'signup_email' => $email
);
$this->session->set_userdata($session);
redirect(base_url('Main/check_login'));
}
else
{
$this->session->set_flashdata('error','Invalid Email or Password');
redirect(base_url('Main/login'));
}
}
public function check_login()
{
if($this->session->userdata() == 'admin#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index2'));
}
elseif($this->session->userdata() == 'owner#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index3'));
}
else
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index'));
}
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}
My Model
public function login_process($email,$pass)
{
//$this->db->select('*');
//$this->db->where('roll_id',$roll);
$this->db->where('signup_email',$email);
$this->db->where('signup_password',$pass);
$query = $this->db->get('signup');
if($query->num_rows() > 0)
{
$this->session->set_flashdata('signup_email');
return true;
}
else
{
return false;
}
}
You missed the parameter here
if($this->session->userdata() == 'admin#gmail.com')
instead it should be
if($this->session->userdata('signup_email') == 'admin#gmail.com')

Unable to call model function from the controller

I am having problem with my code, as below
The error i am is as follow
( ! ) Fatal error: Call to undefined method Agentie_model::get_agentii_list() in C:\wamp\…controllers\fk_controller.php on line 65
Controller:fk_controller
public function __construct() {
parent::__construct();
$this->load->model('agentie_model');
$this->load->model('fk_model');
}
public function add2() {
$this->load->model('agentie_model');
$data['principal'] = $this->agentie_model->get_agentii_list(); //Here is the error(line 65)
$this->load->view('lista_agentii', $data);
}
Model:agentie_model
private $_table = "agentii";
public function get_agentii_list() {
$query = $this->db->get($this->_table);
return $query->result();
}
View:lista_agentii
echo "Lista agentii:</br>";
foreach($principal as $list) {
echo $list->nume_agentie;
}
public function __construct()
{
parent::__construct();
$this->db = $this->load->database('default', true);
}
public function add2()
{
$this->load->model('Agentie_model',true);
$data['principal'] = $this->Agentie_model->get_agentii_list(); //Here
$this->load->view('lista_agentii', $data);
}
in your controller set this below
function fk_controller() {
parent::__construct();
$this->load->model('agentie_model');
$this->load->model('fk_model');
}
public function add2() {
$this->load->model('agentie_model');
$data['principal'] = $this->agentie_model->get_agentii_list(); //Here is the error(line 65)
$this->load->view('lista_agentii', $data);
}
in your model view
class Agentie_model extends CI_Model
{
public function get_agentii_list() {
$query = $this->db->get($this->_table);
return $query->result();
}
}

custom codeigniter library not returning data

I have made a codeigniter user login library but have run into a small issue. When I login it is not returning any of the functions in the library like $this->get_user_username();
On my dashboard I should be able to do this echo $this->user->get_user_username();
I think I know problem is because the if ($this->validate_password($password)) { is around the query. And stops
me returning any data.
Note: I only would like to be able to set the user_id in session which is working. And just return the others as variables etc.
Question Because I am using if ($this->validate_password($password)) { in my login and once logged in it blocks me from retrieving any data what would be best solution so can still use it and retrieve my data
With out setting it all in sessions as I said I only want user id set
in sessions.
Dashboard Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('admin/user');
}
public function index()
{
echo $this->user->get_user_username();
print_r($_SESSION);
$data['top_navbar'] = Modules::run('admin/common/top_navbar/index');
$data['column_left'] = Modules::run('admin/common/column_left/index');
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('common/dashboard_view', $data);
}
}
User Library
<?php
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
}
public function login($username, $password) {
if ($this->validate_password($password)) {
$this->CI->db->where('username', $username);
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->CI->session->set_userdata(array('user_id' => $row->user_id)); // Working
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
} else {
return false;
}
}
public function logout() {
$this->CI->session->unset_userdata('user_id');
$this->user_id = '';
$this->username = '';
}
public function get_user_id() {
return $this->user_id;
}
public function get_user_group_id() {
return $this->user_group_id;
}
public function get_user_username() {
return $this->username;
}
public function get_user_firstname() {
return $this->firstname;
}
public function get_user_lastname() {
return $this->lastname;
}
public function get_user_email() {
return $this->email;
}
public function validate_password($password) {
if (password_verify($password, $this->stored_hash())) {
return $password;
} else {
return false;
}
}
public function stored_hash() {
$this->CI->db->where('username', $this->CI->input->post('username'));
$query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->password;
} else {
return false;
}
}
}
Problem Solved I thought post my own answer, I had to add another section in the construct area of user library to make it work
And then use session userdata for where check.
public function __construct() {
$this->CI =& get_instance();
if ($this->CI->session->userdata('user_id') == TRUE) {
$this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
}
}
Library
<?php
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
if ($this->CI->session->userdata('user_id') == TRUE) {
$this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
} else {
$this->logout();
}
}
public function login($username, $password) {
if ($this->validate_password($password)) {
$this->CI->db->where('username', $username);
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->CI->session->set_userdata(array('user_id' => $row->user_id));
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
} else {
return false;
}
}
public function logout() {
$this->CI->session->unset_userdata('user_id');
$this->user_id = '';
$this->username = '';
}
public function get_user_id() {
return $this->user_id;
}
public function get_user_group_id() {
return $this->user_group_id;
}
public function get_user_username() {
return $this->username;
}
public function get_user_firstname() {
return $this->firstname;
}
public function get_user_lastname() {
return $this->lastname;
}
public function get_user_email() {
return $this->email;
}
public function validate_password($password) {
if (password_verify($password, $this->stored_hash())) {
return $password;
} else {
return false;
}
}
public function stored_hash() {
$this->CI->db->where('username', $this->CI->input->post('username'));
$query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->password;
} else {
return false;
}
}
}

Check if has beign redirected from another controller

On my login controller or view I would like to be able to see if it is possible to check if page has redirected back to login and then display bootstrap error message. I also use a MY_controller function in codeigniter
I do not want to use codeigniter session flash data message. I have my own error message as showing in code.
Is it possible to check if controller has been redirect from another controller?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
$user_id = $this->session->userdata('user_id');
if (isset($user_id)) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard');
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', 'Does not match any of our database records');
return false;
} else {
return true;
}
}
}
MY Controller
<?php
class MY_Controller extends MX_Controller {
public function __construct() {
parent::__construct();
Modules::run('admin/error/permission/check');
}
}
Update I have tried This still displays message even though have not been redirect from another page.
Thanks to #AdrienXL for some great advice. $this->load->library('user_agent'); was the best method.
if ($this->agent->referrer()) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
$this->load->library('user_agent');
if ($this->agent->referrer()) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard');
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', 'Does not match any of our database records');
return false;
} else {
return true;
}
}
}

User edit page not working

When i click on my user edit button <?php echo anchor('users/edit/'. $user->user_id, '<div class="btn btn-primary"><i class="fa fa-edit"></i> Edit</div>');?>
It sends me to http://localhost/codeigniter/codeigniter-blog/admin/users/edit/1 but shows error 404 Page Not Found. '1' example is the user_id
But edit function exists. How can I make my edit function on my users controller work with the user id. So if i click on a certain user it will only update that id row information.
Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_user extends CI_Model {
protected $id;
function getAll() {
$query = $this->db->get('user');
if ($query->num_rows() > 0) {
return $query->result();
return $query->row('user_id');
return true;
} else {
return false;
}
}
public function editUser() {
}
public function getID($user_id) {
$user_query = $this->db->get('user');
if ($user_query->num_rows() == 1) {
return $user_query->row('user_id', $user_id);
return true;
} else {
return false;
}
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('user');
if ($this->session->userdata('isLogged') == TRUE) {
return true;
} else {
redirect('/');
}
}
public function index() {
$this->getList();
}
public function edit() {
$this->load->library('form_validation');
$this->load->model('users/model_user');
$this->form_validation->set_rules('name', 'Name');
$this->form_validation->set_rules('username', 'Username');
if ($this->form_validation->run() == TRUE) {
redirect('users');
} else {
$this->getForm();
}
}
function getForm() {
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$this->load->model('users/model_user');
$data['users'] = $this->model_user->getAll();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
function getList() {
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$this->load->model('users/model_user');
$data['text_enabled'] = "Enabled";
$data['text_disabled'] = "Disabled";
$data['users'] = $this->model_user->getAll();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_list', $data);
}
}
May be you miss to use index.php in url
(OR)
Use below code base_url('');
<?php
$url=base_url('users/edit/'. $user->user_id);
echo anchor($url, '<div class="btn btn-primary"><i class="fa fa-edit"></i> Edit</div>');?>
I can view page now to do with routes $route['users/edit/(:num)'] = "users/users/edit/$1";

Resources