• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
session problem , i want check session active and redirect to login page for all page

#1
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);
    }
Reply

#2
Use a MY_Controller and check for the login in the __construct()
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
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'));
}

}






}
Reply

#4
This is how I do it, change to suit your needs.

PHP Code:
// Controller

/**
 * index ()
 * -------------------------------------------------------------------
 * Check to see if the Admin is logged in.
 * Else show the login view.
 */
public function index()
{
    // if the user is logged in.
    if (logged_in())
    {
        $this->auth->view('dashboard');
    }
    // show the login view.
    else
    {
        $this->auth->login();
    }
}

------------------------------------

// Auth Library

// -------------------------------------------------------------------

/**
 * logged_in ()
 * -------------------------------------------------------------------
 *
 * Check to see if a user is logged in
 *
 * Look in the session and return the 'logged_in' part
 *
 * @return bool
 */
public function logged_in()
{
    if ($this->CI->session->userdata('logged_in') === true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

-----------------------------------------------------

// Helper

// -----------------------------------------------------------------------

/**
 * logged_in
 */
if ( ! function_exists('logged_in'))
{
    /**
     * logged_in ()
     * -------------------------------------------------------------------
     *
     * @return bool
     */
    function logged_in()
    {
        $CI =& get_instance();

        if ($CI->auth->logged_in() == TRUE)
        {
            return TRUE;
        }

        return FALSE;
    }


With the auth_helper it makes it easy for me to use methods in the views for
checking users etc;
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
thank you bro , if i cant i will ask you
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.