0

How do you control from hooks files if the session and the cookies are enabled on user browser?

this is my code unfortunately it doesn't works:

cookie.php (/hooks) class Cookie {

function control_cookies_enabled()
{
 $CI =& get_instance();
 $CI->session->unset_userdata('enabled_cookies',false);
 $CI->session->set_userdata('enabled_cookies','1');

 if($CI->session->userdata('enabled_cookies') !== '1'){
  redirect(site_url('home'));
 }
}

}

then i call that in /config/hooks.php

 $hook['pre_controller'] = array(
                                'class'    => 'cookie',
                                'function' => 'control_cookies_enabled',
                                'filename' => 'cookie.php',
                                'filepath' => 'hooks'

                                );

Thanks.

itsme
  • 48,972
  • 96
  • 224
  • 345

1 Answers1

1

What you need to do is set up a base controller that will look after the session for you and split your logged in controllers from your logged out ones via inheritance. There is no need to do this in a hook.

Please see a previous answer of mine for more information.

Community
  • 1
  • 1
Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • i setted up a controller with 2 methods , first method sets session second controls session but i can't use this envoirment cause it's causing error for too much redirects() , what i need is to redirect to specific url if user disabled the session/cookies. – itsme Oct 12 '12 at 07:13
  • definitely my site should not work without session, and should show an alert page saying : "hey man enable your cookies or you can't go on on this site!" – itsme Oct 12 '12 at 07:14
  • i chcked your answer but i don't like to extend controllers like you do, cause there are hooks were built exactly for what you are doing in controllers ... this is my opinion ;) – itsme Oct 12 '12 at 07:18
  • definitely fixed, as you sad, i used new controller/funct to check if session->userdata('session_id') exist and redirect to error page if not. – itsme Oct 12 '12 at 09:33