I am trying to use session in my project. It works on localhost but not on web server. I read some articles about that in stackoverflow and ellislab, but it didn't work for my project. Probably problem is about creating ancillary classes.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberlogin extends CI_Controller {
//protected $CI;
function __construct()
{
$CI =& get_instance();
parent::__construct();
$CI->load->library('session');
//$this->load->library('form_validation');
//$this->load->helper(array('form', 'url'));
$CI->load->helper('date');
$CI->load->helper('ip_address');
$CI->load->model('Sql_model');
}
Now errors are :
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Line Number: 14
and
A PHP Error was encountered
Severity: Error
Message: Call to a member function library() on a non-object
Line Number: 14
Try this code,
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('date');
$this->load->helper('ip_address');
$this->load->model('Sql_model');
}
If class is in controllers directory, you can directly get the CI instance by extending the CI_Controller and use $this.
Than, try without calling get_instance() function. Just like docs says:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberlogin extends CI_Controller {
//protected $CI;
function __construct()
{
parent::__construct();
$this->load->library('session');
//$this->load->library('form_validation');
//$this->load->helper(array('form', 'url'));
$this->load->helper('date');
$this->load->helper('ip_address');
$this->load->model('Sql_model');
}
This will make you crazy. but it works.
In autoload.php
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','date','ip_address');
and load model in __constructor
public function __construct()
{
parent::__construct();
$this->load->model('Sql_model');
}
Related
I am in the process of upgrading CI from 1.x to 3.x
From my application/controller/home.php which has a class:
class Home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('app_controller');
}
...
And inside application/libraries/App_Controller.php
class App_Controller extends CI_Controller {
function __construct() {
parent::__construct();
$config_app = $this->config->load('Application');
...
From application/config/Application.php I am defining variables:
$config['env'] = 'dev';
$config['https'] = '//'.$_SERVER['HTTP_HOST'].'/';
$config['js'] = '/ui/js/';
But this returns an error
An uncaught Exception was encountered
Type: Error
Message: Class 'CI_App_controller' not found
Filename: C:\xampp\htdocs\myproject\system\core\Common.php
Any suggestions how to overcome this error and why App_Controller is not found.
Thank you!
your libraries shouldn't extend to CI_controller.
What you want to do in that case is move your App_controller into application/core/App_controller.php.
Then go to your application/config/config.php and change your $config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'App_';
Finally your Home controller should extend to App_controller instead like so:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends App_Controller {
function __construct() {
parent::__construct();
$this->load->library('app_controller');
}
}
<?php
session_start();
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class View_job extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Userdb', '', TRUE);
$this->load->model('Locationdb');
$this->load->model('All_functiondb');
$this->load->library('form_validation');
}
}
///
A PHP Error was encountered Severity: Notice
Message: Undefined property: Indexpg::$session
Filename: php_include/authenticate.php
Line Number: 3
Fatal error: Call to a member function userdata() on null in
C:\xampp\htdocs\CodeIgniter-job-site-master\hindusthanjobs\application_candidate_6423\php_include\authenticate.php
on line 3
define session after tis line
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
first thing is that check is session library is loded in controller.
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
I haven't used CodeIgniter in over a year. I remember it was useful for quick, simple projects but I seem to have fallen at the first hurdle here. I can't seem to load my default view. Here is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
private $data = array();
public function __constructor() {
parent::__construct();
}
public function home() {
$this->load->view('header');
$this->load->view('nav');
$this->load->view('home');
$this->load->view('footer');
}
}
This gives me:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 13
But I can't figure out why.
In my config I have set 'html', 'url' and 'form' to autoload. And my routes defaults correctly to 'home'. It's kinda frustrating because I know it's something really simple that I'm forgetting here.
Your __constructor is wrong. Use __construct instead of __constructor
public function __construct()
{
parent::__construct();
}
Your method has same name with the view you are calling which is home
Hi all I am having an error and I just can't see why codeigniter is throwing the error:
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management extends CI_Controller {
public function index(){
//calls login page view
$this->managementView();
}
public function managementView(){
//loads course page view
$users['users'] = $this->management_model->getInfo();
$this->load->view("header");
$this->load->view("users", $users);
$this->load->view("footer");
}
}
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management_model extends CI_Model{
function getInfo(){
$query = $this->db->get("users");
$result = $query->result_array();
return $result;
}
}
I am receiving the following error, I just can't seem to see what the hell I'm doing wrong - I'm new to web stuff so don't know what these errors indicate:
Fatal error: Call to a member function getInfo() on a non-object
also see this:
Severity: Notice
Message: Undefined property: Management::$management_model
Filename: controllers/management.php
Line Number: 12
I can't see the issue? - If anyone could point it out would be much appreciated.
you need to load the model before calling any function from the model.
for eg:
$this->load->model('management_model');
$users['users'] = $this->management_model->getInfo();
or you can load it via the constructor.
function __construct() {
parent::__construct();
$this->load->model('management_model');
}
}
the notice you are getting is clearly telling about the undefined property management model
you can see more about this here
You dont appear to ever set the management_model property in your controller.
I would expect to see something like this somewhere in your controller:
$this->management_model = new Management_model();
Initially you can load the model like this
$this->load->model('management_model');
then only u can use the object to call function
you need some more clarity Click
use
$this->load->model('management_model');
before
$users['users'] = $this->management_model->getInfo();
I have been working on a session validation for my login to make sure that a user is logged in to view pages. I keep getting this error:
Fatal error: Class 'MY_Staffcontroller' not found in /usr/local/var/www/CodeTest
/ci/application/controllers/staff_c.php on line 3
My staff_c page looks like so :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Staff_c extends MY_Staffcontroller {
function homepage()
{
$data['main_content'] = 'homepage_view';
$this->load->view('includes/template', $data);
}
}
I have been reading same questions all over the place and they say the same thing pretty much...
Is your controller located in application/core?
Well yes it is. I can't seem to get passed this hump!
This is the code within My_Staffcontroller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_staffcontroller extends CI_Controller {
function __construct()
{
parent::__construct();
$loggedin = $this->session->userdata('loggedin');
if(!isset($loggedin) || $loggedin != TRUE);
{
die($this->load->view('denied'));
}
}
}
I know this is user error as this is only my second day with CodeIgniter but I can't seem to find proper workaround for this?
I have tried this tutorial and still nothing and also this
Even following this video has me stuck on the session part.
And I just can not get this to work.
Remember Linux is case-sensative whereas Windows is case-insensative.
place you're MY_Staffcontroller inside application/core/MY_Controller.php file
Your MY_Controller.php file should look like this (minus all you're other functions, this is a minimal example)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
class MY_Staffcontroller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function sayHello()
{
echo "Hello, I am a function within MY_Staffcontroller.php";
}
}
Example
This will be located in /application/controllers directory
Basically any protected and public functions located in either MY_Controller OR MY_Staffcontroller will be accessible from derived controllers that extend the extended controller. In this case it would be MY_Staffcontroller
class Public_Staff_Controller extends MY_Staffcontroller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->sayHello();
}
}
/* end of file /application/core/MY_Controller.php */