CodeIgniter Session won't destroy - codeigniter

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.

Related

Codeigniter Redirect or Load view with templates

I don't know how to phrase this question on google so i couldn't find any answers.
In my Views folder, i have templates folder with header,navbar,footer inside.
Whenever i load a view from my controller i would have to do this,
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/pagename');
$this->load->view('template/footer');
How do i do this with redirect? I don't know why but whenever i see code snippets of successful logins or failures they always use the redirect function instead of load view like the above.
for example:
function __construct() {
parent::__construct();
if($this->ion_auth->logged_in()==FALSE)
{
redirect('pages/login');
}
}
or can i use this and will this still be acceptable?
function __construct() {
parent::__construct();
if($this->ion_auth->logged_in()==FALSE)
{
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/login');
$this->load->view('template/footer');
}
}
function __construct() {
parent::__construct();
if($this->ion_auth->logged_in()==FALSE)
{
redirect('controller/login');
}
}
in your controller, create a function called login
function login() {
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/login');
$this->load->view('template/footer');
}
In redirect you need to use controller/method_name
redirect('controllername');
or
redirect('controllername/method');
Rather than construct you can use remap. to redirect if the user is logged or not
REMAP
public function _remap($method, $params = array()){
if(method_exists($this, $method)){
if($this->ion_auth->logged_in()==FALSE){
return call_user_func_array(array($this, $method), $params); //home page
}
return call_user_func_array(array($this, 'login'), $params); //if not logged in
}
show_404();
}
LOGIN
public function login() {
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/login');
$this->load->view('template/footer');
}

Codeigniter User Agent Not Working On Logout Controller

In my MY_Controller.php I have a redirect in there if session has expired.
Then it redirects to the logout controller. I have added a if statement agent->is_referral to the logout controller and set flashdata. Because it has been redirect from dashboard controller it should pick up that if statement and set flashdata but does not.
Question: If the customer is redirected from whatever controller to logout then how to get is_referral() to detect and set the flashdata message. flashdata message is not setting when inside is_referral() if statement.
controllers > account > Logout.php
<?php
class Logout extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->library('user_agent');
if ($this->agent->is_referral()) {
// Does not set the flashdata if redirected from another controller.
$this->session->set_flashdata('warning', 'Your session has expired!');
}
// Flashdata works if have it here.
// $this->session->set_flashdata('warning', 'Your session has expired!');
$this->customer->logout();
redirect('/'); // redirect to login page.
}
}
core > MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if ($this->uri->segment(1)) {
if (!$this->session->userdata('customer_id')) {
$route = $this->uri->segment(1) .'/'. $this->uri->segment(2);
if (isset($route)) {
$ignore = array(
'account/logout'
);
if (!in_array($route, $ignore)) {
redirect('account/logout');
}
}
}
}
}
}
controllers > account > Dashboard.php
<?php
class Dashboard extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('common/header_view');
$this->load->view('account/dashboard_view');
$this->load->view('common/footer_view');
}
}

set_header in MY_Controller codeigniter 3 not working

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

Code Igniter - Unable to load requested file after redirect

I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result

Anti-refresh (F5) submit form in CodeIgniter

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

Resources