Calling model from codeigniter library shows error - php

Hello I have created a library Auth.php in application/libraries to authenticate if the current user is logged in or not. So my controller is :
$session_data = $this->session->userdata('logged_in');
$this->load->library('Auth');
$auth = new Auth;
$user_id = $auth->authenticate($session_data);
And the library :
class Auth{
function authenticate($vars){
$CI =&get_instance();
$CI->load->model('adminmodels/login_model');
$username = $vars['username'];
$password = $vars['password'];
$user_id = $vars['user_id'];;
$user_type = $vars['user_type'];
$check_login = $this->login_model->login($username,$password); //Line 14
if($check_login){
$user_id = $user_id;
}else{
$user_id = 0;
}
return $user_id;
}
}
But it is showing error like :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$login_model
Filename: libraries/Auth.php
Line Number: 14
Whats wrong I am doing ??

While correct generally, in CI you should call the library like this, not with the new keyword:
$this->load->library('Auth');
$user_id = $this->auth->authenticate($session_data);
Also, since you assigned the global CI object to a variable, you can't use $this to refer to it:
$check_login = $this->login_model->login($username,$password); //Line 14
should be:
$check_login = $CI->login_model->login($username,$password); //Line 14
(since you loaded the model there: $CI->load->model('adminmodels/login_model'); )

Inside Codeigniter library file, you cannot load any model, config, or any external library using $this by pass without CI instance object/variable
For your case in Auth.php at line 14, you are wrong :
$check_login = $this->login_model->login($username,$password); //Line 14
It should be like this (if access from internal function only) :
$check_login = $CI->login_model->login($username,$password); //Line 14
Or, if access from any functions in the Auth class :
$check_login = $this->CI->login_model->login($username,$password); //Line 14
But first, you must make CI intance object in your construct function like this :
$this->CI =&get_instance();
$this->CI->load->model('adminmodels/login_model');
before you action in second alternative above.
May help you...
Thanks

Related

Codeigniter 'Message: Undefined index: site_lang' issue at first load

I have developed a website and it is working fine in my localhost.
But I wanted to test it in a local area network before going live.
There is an issue I am facing which is at first load in a browser or any other machine it shows below error until I change the language once. When I change the language once then the issue is never happening again.
I am new and this is my first project so can anyone help me in this regard.
Thanks in advance
A PHP Error was encountered
Severity: Notice
Message: Undefined index: site_lang
Filename: models/companies.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\fp\application\models\companies.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\fp\application\controllers\home.php
Line: 19
Function: get_companies
File: C:\xampp\htdocs\fp\index.php
Line: 315
Function: require_once
Here is my Model:
//---- Table Companies ---
public function get_companies()
{
// Retrieve titles for all languages
$sql = "SELECT * FROM `ci_companies` WHERE `co_id` = 1";
// Retrieve appropriate title according to the chosen language in the system
$sql = "SELECT `co_id`, `co_name`, SUBSTRING(`co_detail`,1,100) AS `co_detail`, `co_img`, `co_img2` FROM `ci_companies` WHERE `co_lang` = '".$_SESSION['site_lang']."'";
$query = $this->db->query($sql);
return $query->result();
}
And here is my controller:
function switchLang($language = "") {
$language = ($language != "") ? $language : "english";
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}
Try like this.error due to wrong use of single quotes.
$language = $this->session->userdata('site_lang');//or $_SESSION['site_lang']
$sql = "SELECT `co_id`, `co_name`, SUBSTRING(`co_detail`,1,100) AS `co_detail`, `co_img`, `co_img2` FROM `ci_companies` WHERE `co_lang` = '{$language}'";

How to 3rd party application access to Yii2 (HumHub)

I want to give a 3rd party PHP application access to Yii2 user data (HumHub) and have tried this:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig));
$user = Yii::$app->user->identity;
return $user;
}
This does not work. There are no errors up until new humhub\components\Application($yiiConfig) but then the 3rd party app breaks with no error thrown and the function does not return anything.
I did find this solution which does not work.
Is there are reason this does not work or is there an alternate solution to getting Yii2 user data properly?
This is how to do it in HumHub V1.0
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge(
require('../protected/humhub/config/common.php'),
require('../protected/humhub/config/web.php'),
(is_readable('../protected/config/dynamic.php')) ? require('../protected/config/dynamic.php') : [],
require('../protected/config/common.php'),
require('../protected/config/web.php')
);
new yii\web\Application($config); // No 'run()' invocation!
Now I can get $user object:
$user = Yii::$app->user->identity;
Indeed an error should be thrown, but, the error settings of your PHP may be overridden or set to not display errors.
You call undefined object Yii::$app->user->identity. The reason, from documentation because you have not initialized the Yii object. So your code should be as follows:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig)); // try to comment this line too if it does not work
// Add The Following Line
new yii\web\Application($yiiConfig); // Do NOT call run() here
$user = Yii::$app->user->identity;
return $user;
}

Codeigniter undefined property on core/Model.php

Controller
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details->name; //THIS IS LINE 28
$data2['item_description']= $item_details->description;
$data2['item_price'] = $item_details->price;
$this->load->view('item/item_edit',$data2);
}
There's an error to my view, and I don't know why
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Item::$name
Filename: core/Model.php
Line Number: 77
Backtrace:
File: C:\xampp\htdocs\itwa213\application\controllers\Item.php
Line: 28
Function: __get
File: C:\xampp\htdocs\itwa213\index.php
Line: 292
Function: require_once
I already checked my autoload on config and it's properly configured with
$autoload['libraries'] = array('database');
You could try this
public function editItem() {
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details['name']; //THIS IS LINE 28
$data2['item_description'] = $item_details['description'];
$data2['item_price'] = $item_details['price'];
$this->load->view('item/item_edit',$data2);
}
Your controller function change to like this
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$data2['item_details'] =$this->ItemModel->edititem($this->input->get('id'));
$this->load->view('item/item_edit',$data2);
}
and in your view page
echo $item_details->name;
You should look at your code for the get function you use. try to replace the $this->input->get('id') to existing id number first. check whether it success or not. If success by using existed id number, so it should your get function not working or something wrong. i'm seldom use $this->input->get() but $this->input->post().
on your edit link, you can use:
edit.
so the id is the third segment uri. use $this->uri->segment(3); to get the third segment and store into $item_id variable into your controller. so, no need to use $this->input->get(); to get the item_id value.

Issue with loading user class

I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:
Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36
Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36
in the game_header.php file I have
$user = new User($_SESSION['id']);
And thought it would work, but sadly it does not.
I have also tried
$user = new User($_SESSION['id']);
on the edit_account.php page, but I was getting the same error.
Here's the edit_account.php code.
Does anyone know what I might be doing wrong here?
include "game_header.php";
$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';
switch($_GET['type']) {
case 'profileoptions' : profile_options(); break;
default : profile_options();
}
function profile_options() {
echo '
';
include 'game_footer.php';
}
When encased into a function you must do the following:
global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.
So it must start with:
function profile_options() {
global $user ;
//The rest of code
}
However, I recommend you to create a parameter:
function profile_options(User $user){
//Much code
}
Then call it where $user is accessible:
profile_options($user) ;

joomla Jfactory does not instantiate itselft in a joomla webapp ( joomla 2.5 )

I am trying to building an external webApp to upload some products in Joomla! with virtuemart.
All works as expected but I have still a problem with Jfactory::getApplication.
I have followed this tutorial:
http://docs.joomla.org/How_to_create_a_stand-alone_application_using_the_Joomla!_Platform
But when I go calling the VmModel on the __construct method it calls a parent::__construct($config):
$this->_cidName = $cidName;
// Get the pagination request variables
$mainframe = JFactory::getApplication() ;
$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
That raises the following error:
vCall to undefined method JException::getUserStateFromRequest()
If could help there is also:
vNotice: Trying to get property of non-object in /home/giuseppe/homeProj/ModaNuovo/libraries/joomla/application/application.php on line 201
raised by:
public static function getInstance($client, $config = array(), $prefix = 'J')
{
if (empty(self::$instances[$client]))
{
// Load the router object.
$info = JApplicationHelper::getClientInfo($client, true);
$path = $info->path . '/includes/application.php';
if (file_exists($path))
The solution is to instantiate the main app together with your one, and if needed also login the following way:
$app = JFactory::getApplication('administrator');
// create a form token
$user = JFactory::getUser(42);
$session = JFactory::getSession();
$hash = JApplication::getHash($user->get('id', 0) . $session->getToken(true));
If needed you have also to perform login to validate this hash.

Categories