1

In my default controller session's value is set. now in my every controller that calls different view, I wants to check if session's value is set then only that view will be shown otherwise it will redirect user to the log in view.
So, as per my knowledge I have to check session's value in my every controller is there any shortcut way?? or is there any way to call one file/function which is called every time before call of any controller. so I can check session's value there.
I had try via hook also like this :

class Authentication extends CI_Hooks {

    var $CI;

    function __construct() {
        $this->CI = & get_instance();
    }

    public function verify_session() {

        if (!isset($this->CI->session)) {
            $this->CI->load->library('session');
            $user_session_id = $CI->session->userdata('email');

            if ($user_session_id == '') {
                redirect('common_controller/home_controller');
            }
        }
    }

}

but it shows me eroor like this : Severity: Notice

Message: Trying to get property of non-object

Filename: hooks/Authentication.php

Line Number: 20

Himanshu
  • 4,327
  • 16
  • 31
  • 39
Sandhya Gor
  • 1,400
  • 3
  • 9
  • 19

2 Answers2

0

Try:

<script>
console.log(<?php echo json_encode($_SESSION, JSON_HEX_TAG); ?>);

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
user3386779
  • 6,883
  • 20
  • 66
  • 134
0

You don't say what framework you're using, but I'm guessing that if you're using MVC that there is a single php file (usually index.php) where you can put your code.

Also it appears to use hooks so you could just add it to a hook that is executed before the controller is identified and called.

Just remember to add a check if you're on the login page and not to apply the redirect if so...

komodosp
  • 3,316
  • 2
  • 30
  • 59