dongpin1059 2016-09-01 03:18
浏览 42
已采纳

页面未正确重定向

I get this kind of error when I create a redirect library I named it redirector.php also I already run it in autoload.php

this is my redirector.php

class Redirector{
    protected $CI;

    public function __construct(){
        //needed for every libraries
        $this->CI =& get_instance();

        $userid = $this->CI->session->userdata('userid');

        if(isset($userid)){
            redirect('/rey');
        }else{
            redirect('/log_in');
        }
    }
}

this is my log_in controller

if($check){
    $arr = array(
        'userid' => $check->id,
        'username' => $username,
        'password' => $password
    );

    $this->session->set_userdata($arr);
    //redirect('/rey','refresh');
}else{
    echo "<p>User does not exist</p>";
}

P.S I just get this kind of error when I make it in library but if I put this code in every constructor every file except log_in.php I didnt get an error

$userid = $this->session->userdata('username');
        if(empty($userid)){
            redirect('/log_in','refresh');
        }

edited

I got no error in redirect in my index which is log_in but if I set the session it didnt redirect to rey.php this is what i have done.

if(empty($userid) && $this->CI->uri->segment(1) != 'log_in'){
            redirect('/log_in');
        }else{
            if(isset($userid) && $this->CI->uri->segment(1) === 'log_in'){
                redirect('/rey');
            }
        }
  • 写回答

1条回答 默认 最新

  • dpiw16824 2016-09-01 03:32
    关注

    Your redirector.php will run before all controllers.. So the code redirect will work before your login controller.. So it will redirected too many times.. Checking login on every controller is better.

    If you still want use this library change your code to this..

    class Redirector{
        protected $CI;
    
        public function __construct(){
            //needed for every libraries
            $this->CI =& get_instance();
    
            $userid = $this->CI->session->userdata('userid');
    
            if(empty($userid) && $this->CI->uri->segment(1) != 'log_in'){
                redirect('/log_in');
            }
    
            if(!empty($userid) && $this->CI->uri->segment(1) == 'log_in'){
                redirect('/rey');
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?