Codeigniter redirect to rigister(404) page - codeigniter

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 {
// ...
}

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;
*/
}

Redirect loop while extend controller 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 :)

codeigniter : load second controller after execution of first controller

we have two controllers one is members.php which is registering the user another one is reserv.php for reserving a ticket for busses.
I want to load second controller after completion of first controller class.
members.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members1 extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
public function index() {
$table = $this->member_model1->insert_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('members');
//$this->load->view('footer');
}
}
reserv.php
<?php
class reserve extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
function index() {
$table = $this->member_model1->get_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('reservation_view');
// $this->load->helper(array('form'));
//$this->load->view('reservation_view');
}
}
?>
To call a controller from another controller, you can use the function redirect() :
// In Members1 controller
redirect('/reserve/index');
Note that you won't be able to send data along with the redirect, such as when you load a view. To send data, you must use $this->session->set_flashdata(); because the redirects seem to use header() (I advise you to have a loot at this post).
// In Members1 controller
$this->session->set_flashdata('key', 'value');
redirect('/reserve/index');
By the way, when I look at your two classes, I wonder why you don't simply create a unique Ticket class with two methods like registerUser() and bookTicket() (these names are pure fancy, be free to use yours).

Codeigniter - uri segment

i need to create an url like this: www.example.com/index.php/scheda/name-surname-id.html
so i create the Scheda controller, this is the code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scheda extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$name_scheda = $this->uri->segment(2);
//i need only the id for the search into db
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}
but when i write the url in the address bar i get an 404 error...can someone help me to understand why?
Your url:
www.example.com/index.php/scheda/name-surname-id.html
Should be:
www.example.com/index.php/scheda/index/name-surname-id.html
index() is the default method, but the index segment can only be missing from the URL if there are no arguments, otherwise Codeigniter will think you are trying to call the method name-surname-id.html().
You can use routes.php or _remap() to clean up the URL and remove the index segment.
// routes.php
$routes['scheda/(:any)'] = 'scheda/index/$1';
OR:
class Scheda extends CI_Controller{
function _remap($method, $args) {
$name_scheda = $method;
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}

Resources