session problem , i want check session active and redirect to login page for all page - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: session problem , i want check session active and redirect to login page for all page (/showthread.php?tid=74787) |
session problem , i want check session active and redirect to login page for all page - said2019 - 11-06-2019 public function login() { $data = array(); if($this->session->userdata('success_msg')) { $data['success_msg'] = $this->session->userdata('success_msg'); $this->session->unset_userdata('success_msg'); } if($this->session->userdata('error_msg')) { $data['error_msg'] = $this->session->userdata('error_msg'); $this->session->unset_userdata('error_msg'); } if($this->input->post('loginSubmit')) { $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); $this->form_validation->set_rules('password', 'password', 'required'); if ($this->form_validation->run() == true) { $con['returnType'] = 'single'; $con['conditions'] = array( 'email'=>$this->input->post('email'), 'password' => md5($this->input->post('password')), 'status' => '1' ); $checkLogin = $this->Model_db->getRows($con); if($checkLogin) { //$this->Model_db->is_logged_in(); $this->session->set_userdata('isUserLoggedIn',TRUE); $this->session->set_userdata('userId',$checkLogin['id_employe']); return redirect(site_url('admin')); //return redirect(site_url('create'),$data); } else { $data['error_msg'] = 'Wrong email or password, please try again.'; //$this->session->set_flashdata('error', 'Invalid Username or Password'); } } } //load the view $this->load->view('users/login',$data); } RE: session problem , i want check session active and redirect to login page for all page - InsiteFX - 11-06-2019 Use a MY_Controller and check for the login in the __construct() RE: session problem , i want check session active and redirect to login page for all page - said2019 - 11-06-2019 I did it but it does not work, I wanted to block page access without an active session; when I close the tab and I retype the link he must have already killed the session to open the login page <?php defined('BASEPATH') OR exit('No direct script access allowed'); class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); //For example I have set logged_in = true in authentication on success //Checking whether userdata logged_in not set ... if true redirecting to login screen if (!isset($this->session->userdata['logged_in'])) { redirect(site_url('login')); } } } RE: session problem , i want check session active and redirect to login page for all page - InsiteFX - 11-07-2019 This is how I do it, change to suit your needs. PHP Code: // Controller With the auth_helper it makes it easy for me to use methods in the views for checking users etc; RE: session problem , i want check session active and redirect to login page for all page - said2019 - 11-07-2019 thank you bro , if i cant i will ask you |