简体   繁体   中英

How to stop redirecting logged-in user to login page when pressing back button in php codeigniter

I have a login for with username and password , after the user logged in , the user redirect to dashboard , and if the user press the back button of browser it will redirect the user to login page , while i want to redirect the user to dashboard as is already logged-in.i would appreciate if anyone can help me to implement that. Here is my login. php controller

<?php
class Login extends CI_controller {
public function __construct()
{
    parent::__construct();
    
    $this->load->library('form_validation');
    $this->load->library('encrypt');
    $this->load->model('login_model');
    

}

function index()
{
    //echo  CI_VERSION; die;
    //echo  $this->encrypt->encode('123456');
    //echo  "<br>".$this->encrypt->decode('ADxTYFI1ATFSYwFn');
    //die;
    $this->load->view('login');

}
function validation()
{
    //  print_r($_REQUEST);
    // die;
  
    $this->form_validation->set_rules('username', 'Username', 'required|trim' );
    $this->form_validation->set_rules('password' , 'Password' , 'required');

    if ($this->form_validation->run())
    {
        
        $res = $this->login_model->can_login($this->input->post('username'), $this->input->post('password'));
        if(is_array($res)){
            $stored_password = $res['stored_password'];
            $row = $res['row'];
            if(password_verify($this->input->post('password'),$stored_password)){
                $userID = $row->id;//die;
                $this->session->set_userdata('admin',$userID);
                redirect('admin/dashboard');
                
            }else{
                $this->session->set_flashdata ('message','Invalid Credentils');
                redirect('admin/login');
            }
        }else{
            $this->session->set_flashdata ('message','Invalid Credentils');
            redirect('admin/login');
        }
        

    }
    else
    {
      $this->index();
    }
  }
 }

And here is my login_model.php

<?php
class Login_model extends CI_Model
{
function can_login($username, $password)
{
    $this->db->where('username', $username);
    $query = $this->db->get('user_login');

    if($query->num_rows() > 0)
    {
        $result = $query->row();
        //print_r($result);die;
        $stored_password = $result->password;
        $return = array('stored_password'=> $stored_password , 'row' => $result);
        return $return;
    }
    else
    {
        return FALSE;
    }

}

And here is my dashboard.php controller

<?php
class Dashboard extends CI_Controller
{
 function __construct()
 {
    parent::__construct();
     if(!$this->session->userdata('admin'))
     redirect('admin');
    // else{
    //  redirect('admin/dashboard');
    // }
}
function index()
{
   $this->load->view('admin/dashboard');
}

function logout()
{  
     $this->session->sess_destroy();
     redirect('admin');
 }

}
    

Solved, I solved the problem by adding session in my login.php controller

public function __construct()
{
    parent::__construct();
    if($this->session->userdata('admin'))
     redirect('dashboard');
    $this->load->library('form_validation');
    $this->load->library('encrypt');
    $this->load->model('login_model');

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM