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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab