How can I create logout button if my login controller with session is like this?
function login_user() {
$user_login = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$data=$this->Infoserbilis_model->login_user($user_login['username'],$user_login['password']);
if($data) {
$session_data['logged_in'] = TRUE;
$this->session->set_userdata($session_data);
//$this->session->set_userdata('logged_in', $session_data);
redirect('Infoserbilis/admin_page', 'refresh');
} else {
echo '<script>alert("Invalid Username or Password");</script>';
redirect('Infoserbilis/index', 'refresh');
}
}
I have tried $this->session->sess_destroy(); on my function logout but to no avail. Thanks in advance
public function logout() {
// Removing session data
$this->session->sess_destroy();
echo '<script>alert("Bye!");</script>';
redirect('Infoserbilis/index', 'refresh');
}
Ok, base on your message, $this->session->sess_destroy() just only destroy the current session, you need redirect page manually. Write the specific route you want to redirect to in redirect() function
/**
* logout
*/
public function logout()
{
$this->session->sess_destroy();
redirect('/');
}
function logout() {
$this->session->unset_userdata('is_searched');
redirect('CONTROLLER/login');
}
You are redirecting to index function in Infoserbilis controller. Check session is active or not in that function.
Here is an example :
public function index() {
if($this->session->userdata('is_logged_in') == true){
//load the required view
}
else {
redirect('controller/login');
}
}
or you can redirect to login from logout function
public function logout() {
$this->session->sess_destroy();
redirect('controller/login');
}
Related
I want to prevent the user to go back after i logged in, how can i do that.. please help im using php and codeigniter
I have this code on my controller
function __construct()
{
parent::__construct();
if ($this->session->userdata('LoggedIn') == FALSE)
{
session_destroy();
redirect('login','refresh');
}
}
I am open to all your help and suggestions guys
if ($this->session->userdata('LoggedIn') == TRUE)
{
redirect('dashboard');
}
Place this in your index function on login controller.
You should try like as below
function __construct()
{
parent::__construct();
if ($this->session->userdata('LoggedIn') == FALSE)
{
session_destroy();
redirect('login','refresh');
}else{
redirect('home','refresh');
}
}
Add below code on your login page controller, So if user came back, It will redirect again.
if ($this->session->userdata('LoggedIn') == TRUE)
{
redirect('homepage');//Your page after login
}
You mean after successful login ,user not allow to load login page again!
So you want to check each login function call the user is exists or not
function __construct()
{
parent::__construct();
if ($this->session->userdata('LoggedIn') == FALSE)
{
session_destroy();
redirect('LoginPage','refresh');
}
}
function yourLoginfun()
{
$LoggedIn = $this->session->userdata('LoggedIn');
if(!empty($LoggedIn))
{
redirect('/user_homepage.php', 'refresh');
}
else
{
$this->load->view('LoginPage');
}
}
I'm working with codeigniter, I'm doing an auth system - user can log in, he can see all pages, after he can log out. The problem is next: why logged out user can see all pages without to be logged in. how can I correct it? This is my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url', 'form'));
$this->load->model("usermodel");
$this->load->library('session');
}
private function view($page, $data=false) {
if($page == "auth/login" ){
$this->load->view("auth/header_auth.php");
}else{
$this->load->view("header.php");
}
$this->load->view($page, $data);
$this->load->view("footer.php");
}
public function index() {
if ($this->session->userdata("user")) {
redirect("dashboard", "refresh");
return;
}
$this->view("auth/login");
}
public function fail() {
$this->view("auth/login");
}
public function dashboard() {
$this->view("auth/dashboard");
}
public function login() {
$login = $this->input->post("login");
$password = $this->input->post("password");
if ($this->usermodel->login($login, $password)) {
$this->session->set_userdata("user", $login);
redirect("dashboard", "refresh");
} else {
redirect("fail", "refresh");
}
}
public function logout() {
$this->session->unset_userdata('user');
session_destroy();
redirect('index', 'refresh');
}
}
you must check if user is logged in at beginning of each function which must have user privileges to view
if (!$this->session->userdata("user")) {
redirect("auth/login", "refresh");
}
I am assuming that your view function can only be viewed if a user is logged in, change
private function view($page, $data=false) {
if($page == "auth/login" ){
$this->load->view("auth/header_auth.php");
}else{
$this->load->view("header.php");
}
$this->load->view($page, $data);
$this->load->view("footer.php");
}
to this
private function view($page, $data=false) {
if (!$this->session->userdata("user")) {
redirect("auth/login", "refresh");
}
if($page == "auth/login" ){
$this->load->view("auth/header_auth.php");
}else{
$this->load->view("header.php");
}
$this->load->view($page, $data);
$this->load->view("footer.php");
}
Update
also your dashboard can be viewed by everyone, to make it viewable only by logged in user do this:
public function dashboard() {
if (!$this->session->userdata("user")) {
redirect("auth/login", "refresh");
}
$this->view("auth/dashboard");
}
this is my controller:-
public function home() {
if($this->session->userdata('id')) {
$this->header();
$this->load->model('admincon');
$e = $this->admincon->select();
$data['e'] = $e;
$this->load->view('admin/home',$data);
} else {
$this->index();
}
}
in the view page of function home() there is a form which submitted through function subques() the function is here:-
public function subques() {
if($this->session->userdata('id')) {
$tp=$this->input->post('box1',TRUE);
$s=$this->input->post('box',TRUE);
$t=$this->input->post('t',TRUE);
$a=$this->input->post('a',TRUE);
$b=$this->input->post('b',TRUE);
$c=$this->input->post('c',TRUE);
$d=$this->input->post('d',TRUE);
$n=$this->input->post('n',TRUE);
$this->load->model('admincon');
$this->admincon->subque($s,$t,$a,$b,$c,$d,$n,$tp);
$this->home();
} else {
$this->index();
}
}
but after submitting the values to the database when user redirected to the page if they click on refresh button the previous data store into the database another time. how to solve this problem.
i mean how to clear the variables after use them.
User redirect() instead of $this->home()
public function subques()
{
if($this->session->userdata('id'))
{
$tp=$this->input->post('box1',TRUE);
$s=$this->input->post('box',TRUE);
$t=$this->input->post('t',TRUE);
$a=$this->input->post('a',TRUE);
$b=$this->input->post('b',TRUE);
$c=$this->input->post('c',TRUE);
$d=$this->input->post('d',TRUE);
$n=$this->input->post('n',TRUE);
$this->load->model('admincon');
$this->admincon->subque($s,$t,$a,$b,$c,$d,$n,$tp);
redirect('controller_name/home');
}
else
{
redirect('controller_name/index');
}
}
without this $this->home(); use
redirect(base_url() . 'controller_name/home');
Hi i m new to ci just working on the login & logout. When i try to logout it
shows url is not found /loginController/logout
What is the problem ? Is it because of the session or did i miss something?
Here is my controller
<?php
class loginController extends CI_Controller{ /**controller*/
public function index(){
$this->login();
}
public function login(){
$this->load->view('login');
}
public function home(){
if ($this->session->userdata('logged')){
$this->load->view('main');
} else {
redirect('loginController/denied');
}
}
public function denied(){
$this->load->view('denied_page');
}
public function login_validation() /**set rules*/
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username',
'Username', 'required|min_length[3]|max_length[12]');
$this->form_validation->set_rules('password',
'Password', 'required|sha1|callback_password_check');
if ($this->form_validation->run()){ /** form valdation*/
$data = array(
'username' => $this->input ->post('username'),
'logged' => 1
);
$this->session->set_userdata($data);
redirect('main');
} else {
$this->load->view('login');
}
}
public function logout(){ //login
$this->session->sess_destroy();
redirect('login');
}
public function password_check(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('users');
if($this->users->log_in($username, $password)){
return True;
}else{
$this->form_validation->set_message('password_check',
'Incorrect username or password.');
return False;
}
}
}
?>
change the code
public function logout(){ //login
$this->session->sess_destroy();
redirect('login');
}
to
public function logout(){
$this->session->sess_destroy();
redirect('loginController/login');
}
load $this->load->helper('url'); on the index function and change $this->load->helper(array('form', 'url')); to $this->load->helper(array('form')); on your validation function.
when redirecting use base_url()
on function logout change the second line redirect('login'); to redirect(base_url('/'.get_class($this)));
i hope it works and help you, just remember to read this and after logout the login page will be accessed via your index() function.
if that will not work just try to change base_url() to site_url() but you should know what is the differences.
I already registered session with my code and preventing the the profile direct access with following code.
public function profile() {
if ($this->session->userdata('logged_in')) {
//var_dump($this->session->userdata['logged_in']);
$session_data = $this->session->userdata('logged_in');
$data['nric_number'] = $session_data['nric_number'];
$this->load->view('templates/header');
$this->load->view('layouts/employee/profile', $data);
$this->load->view('templates/footer');
} else {
//If no session, redirect to login page
$base = base_url();
redirect($base . 'checkinout', 'refresh');
}
}
Suppose, I am going prevent another controller named allusers. So, Script will be something like below-
public function allusers() {
if ($this->session->userdata('logged_in')) {
$this->load->view('layouts/employee/allusers');
} else {
//If no session, redirect to login page
$base = base_url();
redirect($base . 'checkinout', 'refresh');
}
}
But, I would not like to use following condition for each method. Can I skip actually?
if($this->session->userdata('logged_in')) {
} else {
}
Just put the code in your constructor of class, in this way you dont need to check for the session in all methods!
if(!($this->session->userdata('logged_in'))) {
$allowed = array(
'method1',
'method2'
);
if ( ! in_array($this->router->fetch_method(), $allowed) {
redirect('login');
}
}