2015-11-08 04:19
浏览 42

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

Here is my code in controller:

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


And here is my code in views:


   $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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulian8485
    doulian8485 2015-11-08 06:54

    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 is what happen when you try to built two system simultaneously. Anyway thanks for the response and tried to solve it for me.

    点赞 评论
  • douzong3599
    douzong3599 2015-11-08 04:32

    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.

    点赞 评论