Codeigniter extends base class

I want to have a class that checks input on all the controllers that I specified. Codeigniter version is 2.1.0, and I have php 5.3.10

Here's how I installed it: I look at http://ellislab.com/codeigniter/user_guide/general/core_classes.html and I configured it as follows: in the file / application / core / MY _Main.php

class MY_Main extends CI_Controller { function __construct() { parent::__construct(); } } 

In my controller, I have welcome.php

 ?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends MY_Main { function __construct() { parent::__construct(); } public function index() { $this->load->view('welcome_message'); } } 

So, if I put the login in MY-Main, it should get the job, but I can't get it to work for anyone.

+4
source share
4 answers

I needed to add the following code to /application/config/config.php before I get the extenson of the main classes working as described in the CI manual.

Code taken from here http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

 function __autoload($class) { if(strpos($class, 'CI_') !== 0) { @include_once( APPPATH . 'core/'. $class . EXT ); } } 
+4
source

The logic is correct, it should work. This is exactly what I do on all my codeigniter sites. My code is a bit more complicated as my login check is called from the library (so I have to call $CI =& get_instance(); and then $CI instead of $this ), but something like below should work for you. logged_in is simply the name given to the session item when the user logs in.

 class MY_Main extends CI_Controller { function __construct() { parent::__construct(); $session_data = $this->session->all_userdata(); if(!isset($session_data['logged_in'])) redirect('/login'); } } 

As for your comment above (http 500), not quite sure what is going on there. The code you inserted should not throw errors, so something else might be happening. Try enabling encoding built into the logging features.

http://codeigniter.com/user_guide/general/errors.html

+2
source

You must create a library class and put it in your library folder and load it as auto_load or inside your controllers. create functions inside your library, for example:

  /** * * @return boolean check if a user is logged in or not */ function notLogin() { if (!$this->is_logged_in()){ //echo "pelase <a href='login'><b>login</b></a> to continue "; redirect('home/login','refresh'); exit; } return true; } 

and call it inside your controller constructor or any functions you want:

  class Main extends CI_Controller { private $POST = array(); private $ci_form; function __construct() { parent::__construct(); //check if user is logged in or not $this->m_auth->notLogin(); $this->load->library('form_validation'); $this->load->library('ajax_pagination'); } } 
+1
source

This is due to a database connection.

Please check if your database is:

  • was selected by including error reports from your cpanel error log. User
  • added to your database.
0
source

Source: https://habr.com/ru/post/1397271/


All Articles