简体   繁体   English

页面未正确重定向

[英]The page isn’t redirecting properly

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 创建重定向库时,我将其命名为redirector.php,并且已经在autoload.php中运行该错误。

this is my redirector.php 这是我的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 这是我的log_in控制器

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>";
}

PS 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 PS:当我在库中创建代码时,我只会遇到这种错误,但是如果我将这段代码放在每个构造函数中,除了log_in.php之外的每个文件,我都不会出错

$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. 我在索引为log_in的重定向中没有出现任何错误,但是如果我设置了会话,则它没有重定向到rey.php,这就是我所做的。

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');
            }
        }

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. 您的redirector.php将在所有控制器之前运行。.因此,代码重定向将在您的登录控制器之前运行。.因此它将重定向太多次。.检查每个控制器上的登录更好。

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');
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM