1

I'm totally stuck here, I created a controller login which it get, set rules and authentication with DB and at the end of code it set session. Also I have another controller dashboard which displays dashboard and also must use session to see if session is set, if true user can use the controller else redirect to login page. that's it But I can't get session in dashboard, what should I do here?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller
{

    /**
     * Login constructor.
     */
    public function __construct()
    {
        parent::__construct();

        //Load Form helper
        $this->load->helper('form');

        //Load form validation library
        $this->load->library('form_validation');
    }

    /**
     *Display Login page
     */
    public function index()
    {

        $title['title'] = 'Admin login';
        $this->load->view('login', $title);
    }


    /**
     *
     * Set rules for authentication
     *
     * @return bool
     */
    public function rules()
    {

        $rules = array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'remember',
                'label' => 'Remember',
                'rules' => 'trim|int|xss_clean'
            )
        );

        $this->form_validation->set_rules($rules);

        if ($this->form_validation->run() != FALSE) {
            $this->authentication();
        } else {

            $title['title'] = 'Admin login';
            $this->load->view('login', $title);
        }
    }

    /**
     * Check if user is exist
     */
    public function authentication()
    {

        $data = array(
            'username' => $this->input->post('username', true),
            'password' => $this->input->post('password', true)
        );

        $this->load->model('userData');
        if ($this->userData->getUserData($data) == FALSE) {

            $title['title'] = 'Admin login';
            $this->load->view('login', $title);
        } else {

            $this->set_session($data);
            redirect('dashboard/index');
        }

    }

    /**
     * @param $data username|password
     */
    public function set_session($data)
    {

        $session_data = array(
            'username' => $data['username'],
            'login' => true
        );

        $this->session->set_userdata('auth', $session_data);
    }

}

and here is dashboard:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Dashboard extends CI_Controller{

    public function __construct()
    {
        parent::__construct();

    }

    /**
     * Display dashboard 
     */
    public function index(){


        $array = $this->session->userdata('auth');
        $login = $array['login'];

        if ($login == true) {
            $title['title'] = 'Dashboard';
            $this->template->load('panel/dashboard', $title);
        }else{
            redirect('login/index');
        }     
    }
}

1 Answer 1

0

You are doing wrong approach. You cant access $_post values outside the function. As well you no need authentication() function.

When you call this function if ($this->form_validation->run() != FALSE) and if its PASS, then add the ``authentication()` function codes to here. Then you can set the session.


Sample of code

$this->form_validation->set_rules($rules);

if ($this->form_validation->run() != FALSE) {
    $data = array(
        'username' => $this->input->post('username', true),
        'password' => $this->input->post('password', true)
    );

    $this->load->model('userData');
    if ($this->userData->getUserData($data) == FALSE) { # Uncler this point

        $title['title'] = 'Admin login';
        $this->load->view('login', $title);
    } else {

        $this->set_session($data);
        redirect('dashboard/index');
    }
} else {

    $title['title'] = 'Admin login';
    $this->load->view('login', $title);
}
3
  • if ($this->userData->getUserData($data) == FALSE) it's result of model class that select data from db. But there is another point here I can echo session in login controller with this method but why it doesn't work in dashboard?
    – nima
    Jul 1, 2016 at 19:44
  • @nima do it my way Jul 1, 2016 at 19:45
  • problem is with here if ($this->userData->getUserData($data) == FALSE) im sure Jul 1, 2016 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.