I have extending ci_controller like this on application/core
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
}
class Public_Controller extends MY_Controller {
public $layout = 'layout';
}
class Surveyor_Controller extends MY_Controller {
public $layout = 'surveyor/template/layout';
public function __construct() {
parent::__construct();
$login_status = $this->session->userdata('login_status');
$group = $this->session->userdata('group');
if (($login_status !== true) && ($group !== 3)) {
redirect(base_url());
}
// Pastikan hanya "operator" yang boleh mengakses.
if ($group !== '3') {
$this->session->set_flashdata('error', 'Anda tidak berhak mengakses halaman ini!');
redirect(base_url());
}
}
}
My question is, back arrow page is still working
So, in application/controller : i made a new class like this :
class C_surveyor extends Surveyor_Controller {
public $data = array(
'halaman' => 'home',
'main_view' => 'surveyor/v_home'
);
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->model('m_surveyor');
}
public function index() {
$this->load->view($this->layout, $this->data);
}
Just say user has on index, and clik arrow back so get login page. Header not working. When user click right arraow, it still get index page.
Where am I missing ?
Any solution it so appreciated
Try adding this code instead of that 2 lines,
$this->output->set_header("HTTP/1.0 200 OK");
$this->output->set_header("HTTP/1.1 200 OK");
$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
Refer This
Related
After right login details,form redirects to same place
This is my controller for the login
The login should be redirected to the dashboard after validation and database check
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
if($this->session->userdata('logged_in')) {
redirect(base_url().'dashboard');
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
redirect(base_url().'dashboard');
}
}
$this->load->view('Templates/header', $template);
$this->load->view('Login/login_form');
$this->load->view('Templates/footer');
}
}
code for Dashboard
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('dashboard_model');
if(!$this->session->userdata('logged_in')) {
redirect(base_url());
}
}
Try This Method
redirect('dashboard');
Try site_url() as well. Make sure there are proper code(error free) of view in dashboard controller.
redirect(site_url()."/dashboard");
i'm try to get news_id from database but when go to view say this error :
Trying to get property of non-object / Message: Message: Undefined variable: xls
model:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mdl_tagihan extends CI_Model { public function employeeList() {
$this->db->select(array('e.id', 'e.IDTAGIHAN', 'e.BANDWIDTH_BILLING', 'e.SITE_NAME', 'e.PERIODE_TAGIHAN',
'e.REGION', 'e.NOMINAL', 'e.USED_FOR','e.RECON_PERIOD','e.OA_DATE','e.REQUEST_ID','e.TRANSMISSION_ID','e.PROVIDER','e.PRODUCT',
'e.SOW', 'e.NE_ID', 'e.NE_NAME', 'e.FE_ID', 'e.FE_NAME', 'e.BANDWIDTH', 'e.SERVICE_2G', 'e.SERVICE_3G', 'e.SERVICE_4G', 'e.TOTAL_SERVICE'));
$this->db->from('import as e');
$query = $this->db->get();
return $query->result_array();
}
controller:
class Ctrl_tagihan extends CI_Controller { public function __construct()
{
parent::__construct();
if($this->session->userdata('group') != '1'){
$this->session->set_flashdata('error','Maaf, login first!');
redirect('CTRL_Login');
}
$this->load->model('Mdl_tagihan');
$this->load->helper(array('form', 'url'));
$this->load->library('upload');
}
public function index()
{
$data['tagihan'] = $this->Mdl_tagihan->get_tagihan();
$this->load->view('admin/dbtagihan/index_tagihan', $data);
}
public function export_excel(){
$data = array( 'IDTAGIHAN' => 'Laporan Excel',
'dbtagihan' => $this->Mdl_tagihan->listing());
$this->load->view('admin/dbtagihan/laporanexcel_tagihan',$data);
}
view:
<?php ("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$xls.xls.xls"); ("Pragma: no-cache"); ("Expires: 0") ?> <table border="1" width="100%"> <thead> <tr>
In order to use the $xls on your view, you have to supply a $xls variable on the controller :
public function export_excel(){
$data = array( 'IDTAGIHAN' => 'Laporan Excel',
'dbtagihan' => $this->Mdl_tagihan->listing());
$data['xls'] = 'Title';
$this->load->view('admin/dbtagihan/laporanexcel_tagihan',$data);
}
This is the complete code (I want to select the view through a variable set on the MY_Controller):
I would like to pass that variable but it doesn't "reach" the view it gives me
$ses_group = "not_logged_in" with a test echo and I'm not setting anything on the MY_Controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
$this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
$this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
$this->special_data['ses_group'] = 'admin';
}else{
// $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}
The Main_Controller:
function index(
$data = array(
'ses_group' => $this->special_data
);
$this->load->view('auth/descricao_anuncio', $data);
)
The view:
<?php if($ses_group="not_logged_in"){ ?>
<li>Login</li>
<?php }elseif($ses_group="users"){ ?>
<li>Your Area</li>
<li>Logout</li>
<?php }elseif($ses_group="empresas"){ ?>
<li>Empresa</li>
<li>Logout</li>
<?php }?>
echo $ses_group;
Thanks againg!
Controller
<?php
class MY_Controller extends CI_Controller {
protected $special_data = array();
function MY_Controller () {
parent::Controller();
}
function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}
}
You can send your value from controller to views by using $this-> in your MY_Controller
Controller
class Main_controller extends MY_Controller {
function __construct() {
parent::MY_Controller();
}
$this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
$this->load->view('view_x', $this->data);
}
Views
<?php echo $group; ?>
Is this what you need
controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}
class Main_controller extends MY_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$data = array(
'group' => $this->special_data('a')
);
$this->load->view('view_x', $data);
}
}
view
<?php echo $group; ?>
I have got my login function to work which then has an array to show the session data but when I click logout it redirects me to the login page but does not clear the session data if I manually load up the members page.
Controller Function
public function logout(){
$this->session->sess_destroy();
redirect('site/login');
}
public function members(){
if ($this->session->userdata('is_logged_in')){
$data['title'] = "Members";
$this->load->view('view_header');
$this->load->view("view_members", $data);
}
else{
redirect('site/restricted');
}
}
Model
class Model_users extends CI_Model {
public function canLogin(){
$this->db->where('Username', $this->input->post('Username'));
$this->db->where('Password', md5($this->input->post('Password')));
$query = $this->db->get('user_registration');
if ($query->num_rows() == 1){
return true;
}
else{
return false;
}
}
}
Members View
<?php
echo "<pre>";
print_r ($this->session->all_userdata());
echo "</pre>";
?>
<a href='<?php echo base_url()."index.php/site/logout"?>'>Logout</a>
Not sure what I'm missing here since the logout function runs but doesn't seem to clear the session.
Try create a function in your Home controller cleaning the cache like this:
function clear_cache()
{
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
So call it from your controller's constructor
class Home extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->clear_cache();
}
}
Hope it help.
How to prevent refresh form page in CodeIgniter?
If I use redirect — all fine, but I can directly appeal to page site.com/update/success.
How can I prevent direct access to success page (only from site.com/update/) ?
Controller update.php
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
redirect('/update/success');
}
}
public function success() {
$message = 'Your profile has been successfully updated';
$this->load->view($message);
}
You could set a token in flashdata in your index() function and then check for that token in your success() method.
class Update extends CI_Controller {
property $token;
public function __construct()
{
$this->load->library('session');
}
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
$this->session->set_flashdata('update_token', time());
redirect('/update/success');
}
}
public function success() {
// Make sure this request came from the index() method...
if( ! $this->session->flashdata('update_token'))
{
redirect();
}
$message = 'Your profile has been successfully updated';
$this->load->view($message);
}
}
It's a time ago i used codeigniter so im not sure.
maybe you can make the succes function private and just call it in the else like this:
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
$this->success();
}
}
private function success() {
$message = 'Your profile has been successfully updated';
$this->load->view($message);
}
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
// create success session/cookie
$this->_success()
}
}
public function _success() {
// destroy success session when called
// checks success session if existing if not the page has been refreshed redirect
$message = 'Your profile has been successfully updated';
echo $this->nocache();
$this->load->view($message);
}
public function _nocache()
{
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
YOu can add '_' underscore so that it is not accessible by url, and add a no cache to the headers so that the page will not stay cache on the browser when they click back or access the same url again.
code not tested