使用codeigniter 3.0,当id为null时,会话重定向到home

Here is my code in controller:

$newdata = array(
   'uid' => $usern,
   'ou' => $filter
);

$this->session->set_userdata($newdata);                             
$this->masterpage->view('User/homeuser');

And here is my code in views:

<?php

   $user = $this->session->all_userdata();

   if ($this->session->userdata('uid')=='') {
      redirect('main/main', 'refresh'); 
   }

?>

However when I type link: http://localhost/[system_name]/index.php/[controller_name]/index

(link to show when uid is null, the page should redirect to main)

Only blank page is appearing. It seems it didn't redirect to the main/main page. Can anyone help me ? Thanks.

This is the most silly things I've done. I just forget to import session from library. So should in my public function I put this function.

$this->load->library('session');

This is what happen when you try to built two system simultaneously. Anyway thanks for the response and tried to solve it for me.

You can not do redirect in views. Use controllers for this, so move your logic to check session data from view to the controller.

View is rendered at the very end of your request so if there is some output, php will not redirect you to anywhere. You see blank page probably because you turned off errors display, but I am sure if you check php error log you'll see there is an error "can not modify headers" or so.

If you deadly want to do redirect in views, use Javascript for that.