I have this on my MY_Controller that was located on my core folder.
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function is_logged_in($data){
$session = $this->session->userdata();
if($session['isloggedin']['username'] == ''){
return isset($session);
}else{
return FALSE;}
}
}
?>
I'm pretty sure i copy pasted the above code from some tutorial and i haven't gotten to editing it based on my needs.
Any case i have questions.
So i have a pages controller that will be responsible for giving access to some views depending on the account_type of the logged in user.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class pages extends MY_Controller {
}?>
this is my session whenever a user logs in.
$new_session = array( 'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'type' => $this->input->post('type'),
'logged_in' => TRUE);
$this->session->set_userdata($new_session);
How do i call the MY_controller function is_logged_in() from the pages controller or is the 'extends MY_Controller' automatically also calls the function is_logged_in() or do i have to basically just put it in a __construct so it automatically calls the function?
Also, how do i go about checking if a user is logged in and seeing their details?
Do i pass session_data from my controller to MY_Controller? if so, how?
Or should i just put a $this->session->userdata(); line inside the is_logged_in() function?
P.S. i have tried using Authentication Libraries but they include far too much to what i need, i just need a basic authentication. Any suggestions? that is still maintained right now.
you can directly call is_logged_in() function from your pages controller. just like this:
Pages.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends MY_Controller {
function __construct() {
parent::__construct(); // this will trigger the __construct() of MY_Controller
}
}
MY_Controller.php
<?php
class MY_Controller extends CI_Controller{
public function __construct() {
parent::__construct();
if( $this->is_logged_in() ) {
// do something if user is allowed to access.
}
else {
// do something if user is not allowed to access
}
}
}
Related
when i code href="<?= base_url(loket/index_pos_bakum) ?>" then run it, it run normali. im directed to base_url/loket/index_pos_bakum. but the problem comes when i type code like before with deffrent value, base_url(admin/loket_admin). it doesnt gonna redirect to admin/loket_admin but its redirect to base_url/loket/admin/loket_admin. the first controller name always appear when im gonna try to redirect to any controller.
defined('BASEPATH') or exit('No direct script access allowed');
class Loket extends CI_Controller
{
public function index_pos_bakum()
{
$data['loket'] = $this->db->get_where('nomor_antrian', ['id_loket' => 0])->result_array();
$data['antrian'] = $this->db->query("SELECT MAX(nomor) AS nomor FROM nomor_antrian WHERE id_loket = 0 ")->row_array();
$this->template->load('template', 'operator/v_pos_yankum', $data);
}
}
And this is my second controller
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Admin_model', 'a_model');
}
public function loket_admin()
{
$this->template->load('template','admin/index_loket');
}
Using CodeIgniter, I have a few custom controllers:
MY_Controller - this extends the CI_Controller
Front_Controller - extednds MY_Controller, used for any "user" facing pages
Admin_Controller - extends MY_Controller, used for "admin" pages
Now, when I am on the admin "index" page, the controller of which uses Admin_Controller, I am getting logged errors coming from the Front_Controller.
I put a var_dump() into the Front_Controller and I cannot see it on the admin page, so I'm confident it is not being called by my code, but somehow, it does seem to be getting called!
Is there any explanation for this?
Some code:
Admin_Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Admin_Controller extends MY_Controller
{
public function __construct() {
parent::__construct();
if(!$this->ion_auth->logged_in()) {
redirect('login', 'location');
} elseif($this->ion_auth->logged_in() AND !$this->ion_auth->in_group(array('admin', 'mod'))) {
redirect('account/dashboard', 'location');
} else {
$this->user = $this->ion_auth->user()->row();
}
$this->data = array(
'head' => 'inc/head',
'foot' => 'inc/foot',
'nav' => 'admin/inc/nav',
'flash' => 'inc/flash_messages',
'user' => $this->user
);
}
}
Admin index:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Index extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function index()
{
$this->load->vars($this->data);
$this->load->view('admin/index');
}
}
MY_Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->data = array(
'head' => 'inc/head',
'foot' => 'inc/foot',
'nav' => 'inc/nav',
'flash' => 'inc/flash_messages',
);
$this->load->model('new/resellers/reseller_model');
$this->load->model('new/white_labels/white_label_model');
// find matching domain and show logo
$domain = $_SERVER['SERVER_NAME'];
$reseller = $this->reseller_model->get_by('domain', $domain);
if($reseller):
$resellerWhiteLabel = $this->white_label_model->get($reseller->white_label_id);
if($resellerWhiteLabel):
$this->data['portal_reseller'] = $reseller;
$this->data['portal_reseller_white_label'] = $resellerWhiteLabel;
endif;
endif;
$this->load->library('lib_log');
$this->load->library('ion_auth');
$this->load->helper('language');
$this->lang->load('auth');
$this->lang->load('site');
}
}
As far as I can remember CodeIgniter is a singleton, so it loads everything on each request.
This means that the Front_Controller will get loaded even if not called. So if there are errors in it they will out.
Take some time, fix the errors and all should be good.
I have some controller in Codeigniter like this
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
// Load globals
$data->mail = new phpmailer();
$data->minified = new Minifier($vars_minified);
$this->load->vars($data);
}
public function index()
{
$this->home();
}
public function home()
{
$data['meta_title'] = seo::text($this->lang->line('home_title'), 70);
$data['body_render']='view_home';
$this->load->view("/layouts/view_layout", $data);
}
}
This is Home controller, that has __construct
Now i want to make new controller like page, but problem is with contruct , do i must repeat __construct in that controller too, or i just can put it somehow that all controllers will us that contsruct?
This is page controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
// Load globals
$data->mail = new phpmailer();
$data->minified = new Minifier($vars_minified);
$this->load->vars($data);
}
public function index()
{
$this->page();
}
public function page()
{
$data['meta_title'] = seo::text($this->lang->line('home_title'), 70);
$data['body_render']='view_page';
$this->load->view("/layouts/view_layout", $data);
}
}
You see, i have again __construct at top, is it possible to get rid of it, and make one unique __construct?
Yes it is possible please follow this guide.
When done, you can set your __construct for all Public_Controller[s], Admin_Controller[s] etc.
It is exactly what you want.
Note that after this step your variables that you send to views are going to change a bit: from $data['key'] , to $this->data['key']consider your data as "global" in scope of either Public_Controller or Admin_Controller etc.
If any troubles write a comment or read this core extending guide by ellislab or this SO thread where I mention this very same method.
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 */
I am attempting to create a constructor for my controller that references a function that I have contained in a helper which is autoloaded.
The function checks whether or not the user is logged in, if so it redirects them to the login page.
It appears that I have not setup the construct correctly as I am receiving the following error:
Fatal error: Call to undefined method Profile::is_logged_in()
This is the controller:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
//function inside autoloaded helper, check if user is logged in, if not redirects to login page
$this->is_logged_in();
}
public function index() {
echo 'hello';
}
}
I only want to make function within the controller accessible if the user is logged in.
This is the helper which is autoloaded
$autoload['helper'] = array('url','array','html','breadcrumb','form','function','accesscontrol');
(accesscontrol_helper.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
//$this->load->view('login_form');
}
}
Why would I not be able to run the function? Is containing the code in the helper the best method?
As other already mentioned, helpers are simply a collection of functions. Expanding on them:
since they're loaded more than once sometimes, you need to specify not to declare a function if already present, all you'll raise an error.
You cannot, moreover, call a CI's class inside them without first instantiating the main CI object. This is a more proper way to use your helper function:
if(!function_exists('is_logged_in'))
{
function is_logged_in()
{
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
}
}
}
I would also have it return instead of echo, and move the die() to the controller, but this is another story.
Helpers are just included functions, so you don't need to access it with $this. Just call it as a normal function:
is_logged_in();
You don't call a helper function using $this. Just do is_logged_in();
public function __construct()
{
parent::__construct();
//function inside autoloaded helper, check if user is logged in, if not redirects to login page
is_logged_in();
}
accesscontrol_helper.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Accesscontrol_helper{
function is_logged_in()
{
//code
}
}
in Profile controller:
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
Accesscontrol_helper::is_logged_in();
}
}