[英]Redirect loop in Codeigniter with single redirect from base controller
I have this base controller: 我有这个基本控制器:
class TCMS_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
if( ! $this->session->userdata('logged_in')){
redirect('admin/authenticate/login');
}
//Loop to get all settings in the "globals" table
foreach($this->Settings_model->get_global_settings() as $result){
$this->global_data[$result->key] = $result->value;
}
}
}
So there I have this basic redirect: 所以我有这个基本的重定向:
redirect('admin/authenticate/login');
if user is not logged in. 如果用户未登录。
Also I have this settings to remove index.php
from urls: 我也有此设置从网址中删除
index.php
:
.htaccess: 的.htaccess:
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
And next config settings: 以及下一个配置设置:
$config['base_url'] = 'http://something.herokuapp.com/';
$config['index_page'] = '';
And when I'm trying to access admin section that has next address: 当我尝试访问具有下一个地址的管理部分时:
http://something.herokuapp.com/admin/controller/method
And if I'm not logged in, I supposed to be redirected to login
page: 如果我没有登录,我应该重定向到
login
页面:
http://something.herokuapp.com/admin/authenticate/login
But instead i get a redirect loop 但是相反,我得到了一个重定向循环
ERR_TOO_MANY_REDIRECTS
How do I fix it? 我如何解决它?
The page: http://tcms.herokuapp.com/ 页面: http : //tcms.herokuapp.com/
The admin section: http://tcms.herokuapp.com/admin/authenticate/login http://tcms.herokuapp.com/admin/dashobard 管理员部分: http : //tcms.herokuapp.com/admin/authenticate/login http://tcms.herokuapp.com/admin/dashobard
I am sure your admin
(may be authenticate
if admin is your folder name) controller also extends TCMS_Controller
. 我确定您的
admin
(如果您的文件夹名称为admin,则可以通过authenticate
)控制器也扩展了TCMS_Controller
。 So when it redirects your admin
controller it executes TCMS_Controller
's construct function again and again redirect to admin controller which cause infinite loop. 因此,当它重定向您的
admin
控制器时,它将再次执行TCMS_Controller
的构造函数,并再次重定向到管理控制器,这将导致无限循环。
To sovle this you need to make a Login
controller which does not extends TCMS_Controller
just extends CI_Controller.And redirect to this controller if user is not loged-in. 要解决这个问题,您需要制作一个不扩展
TCMS_Controller
的Login
控制器,而只扩展CI_Controller。如果用户未登录,则重定向到该控制器。
Well the problem that it's bad practice to have redirects in basic controller, because it can cause redirecting loops: 这个问题是在基本控制器中进行重定向是不好的做法,因为它可能导致重定向循环:
See: Codeigniter Redirect Loop for user session is logged in 请参阅: 登录用于用户会话的Codeigniter重定向循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.