Redirect loop while extend controller Codeigniter - codeigniter

I have a problem in login application in codeigniter , the problem is like this:
I create MY_Controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then I extended it on my library that i created, this is my "Petugas_Controller" library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('petugas_m');
} // end contructor.
public function check(){
if ($this->uri->uri_string() !== 'index' && ! $this->petugas_m->is_logged_in() == TRUE){
redirect('admin/petugas/index');
}
}
}
I'm using that library to be used in my "Petugas" controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('petugas_controller');
$CI = &get_instance();
$CI->check();
}
public function index(){
echo "You have to login";
}
public function dashboard(){
echo "Welcome to dashboard";
}
}
In "Petugas_Controller" library, I loaded model that is called "Petugas_m", here is the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_m extends CI_Model {
public function __construct(){
parent::__construct();
}
public function is_logged_in(){
return FALSE;
}
}
That's all what i was trying, and my problem is I want to check if username is logged or not, by doing $this->check() in "Petugas_library" that loads "Petugas_m" model, i could see an dashboard echo if the "check" method in "Petugas_m" is set to TRUE but it's always redirect me a loop if it FALSE.
I was searching for tutorials and still have this crazy problem :( ,
Thank in advance, and sorry for my bad english.

As long as your petugas_controller library is located within the libraries folder, you can access the check method via
$this->petugas_controller->check();
Also try changing the if statement in your check to something more like this
if ($this->uri->uri_string() !== 'index' && $this->petugas_m->is_logged_in() !== TRUE){

lol, I've just realized my mistake here, I've just tried to make another controller, it's called login, then in my "petugas_controller" library I redirect the user if not login to this "login" controller, it's totally worked ...
Youy guys are inspiring me so much,Thanks :)

Related

How to use session variable in every method by only declaring in class

I want to set my session variable in every function of the class. If the session is not set in any of the function , then it should be redirected to home page
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
public function masteradmin()
{
if($this->session->userdata('name')){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
else
{
redirect('/', 'refresh');
}
}
public function reseller()
{
}
}
Use constructor for that. In OOP constructor is the first method of any class which is called. So, what you can do is, create a constructor and check for the session variable. If the variable exists, do nothing, it will automatically call the function. If the session variable does not exist, redirect the user to the home page.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
function __construct(){
parent::__construct();
if(!$this->session->userdata('name')){
redirect('/home');
}
}
public function masteradmin(){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
public function reseller(){
}
}
?>
create super class in core folder like MY_Controller.php
and there you can define session and then call that super class in every controller extends. for example:
class App_config extends MY_Controller {
}

How to redirect old php routes in codeigniter

I have old urls like http://example.com/products.php?s_productid=231 and now in my proyect whit url would be http://example.com/products/ver/231
How can I make a rule in routes.php to redirect?
Thanks
you don't need to do any change in routes.php, all you need is to create a Controller "Product", with one Method "ver", which receives 231 (it's id I guess) as a parameter, like this:
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
// do your things
}
I hope it helps.
You may need to set the routes in config/routes.php
$route['product/ver/(:num)'] = 'product/ver/$1';
http://example.com/index.php/products/ver/231
Then the controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
/*
echo $id;
*/
}

CodeIgniter + PHPExcel: What is best way to call this library to CodeIgniter?

I have PHPExcel, and I want to add it to CodeIgniter.
Where is the best to copy the files from all CodeIgniter folders, and how do I call it in my controllers/models for regular usage?
EDIT:
It's a whole folder that I need to move, and call the main file: PHPExcel\IOFactory.php
You may add PHPExcel to library in Codeigniter, E.g:
application > libraries > PHPExcel.php
Let say in PHPExcel.php library file:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class PHPExcel
{
public function __construct()
{
//
}
public function some_function()
{
return 'some_function';
}
}
To call PHPExcel library, in your Controller. Let say I named it as
My_controller.php:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class My_controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//Call PHPExcel class
$this->load->library('PHPExcel');
}
public function index()
{
echo $this->PHPExcel->some_function();
}
}
I followed this tutorial: https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
Got my answer !

Codeigniter redirect to rigister(404) page

class Welcome extends CI_Controller{
function checker(){
if (!$_SESSION['bla']) {
$data['include'] = "REGISTER"; //first result
}else{
$data['include'] = "template";
}
$this->load->view('template', $data); //second result
}
//STOP HERE IF RESULT IS FIRST
function index(){
$data['include'] = "index";
$this->load->view('template', $data);
}
}
I want if in my page dont have session all user redirect to 'REGISTER' page, all classess, all functions to redirect in REGISTER page if dont have session. BUt if user has session just can use site normal. HOw to do this ?
I dont want in every class to put this check ... THanks
Create a controller MY_Controller in application/core/MY_Controller.php and extend the CI_Controller like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
function __construct() {
parent::__construct();
// do the checking here...
// if condition fails redirect...
}
}
Extend this MY_Controller class in your every controller instead of CI_Controller so in every request the checking would take place at first. For example:
class User extends MY_Controller {
// ...
}

Model Fails To Load

I am having issues with my Code Ignitor, in that I load my model the code fails, without leaving any trace.
I am attempting to load my model like so
<!--CONTROLLER-->
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Concierge extends Public_Controller {
public function submit()
{
$this->load->model('Concierge_model');
}
My model is set up as below:
<!--MODEL-->
<?php
class Concierge_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function saveRequest($action, $owner)
{
$query = $this->db->query('select * from table');
return $query;
}
Does anyone see what might be the issue that is causing my problem? I am using CodeIgnitor 2.1.2.
Assuming Public_Controller is something custom, make sure that class extends CI_Controller and also make sure the parent's contruct function is called there.
class Public_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
I don't know what the problem was, I deleted the model, view, and controller and recreated them. This fixed the issue, but sadly didn't shed any light onto what caused it.

Resources