我在子域上有一个 CodeIgniter 项目。现在,当我访问 sub.example.com
时,它会加载一个 login page
并在成功登录后重定向到仪表板。登录并建立 session 后,访问 sub.example.com/login/
将自动重定向到 仪表板页面
。现在这是我的问题。成功登录后,访问 sub.example.com
不会重定向到任何地方,它只是加载登录页面。但是访问 sub.example.com/index.php
会将我重定向到仪表板页面。
由于某种原因,我的索引方法被调用或正常工作。
这是我的代码。
.htaccess
IndexIgnore *
php_value date.timezone Asia/Kolkata
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
## Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://sub.example.com/$1 [R=301,L]
## Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
config.php
if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
$config['base_url'] = "http://" . $_SERVER['SERVER_NAME'];
} else {
$config['base_url'] = "https://" . $_SERVER['SERVER_NAME'];
}
routes.php
$route['default_controller'] = 'root';
$route['root_controller'] = 'root';
/*API CONTROLLER*/
$route['api_controller'] = 'api';
/*Guest Controller*/
$route['guest_controller'] = 'guest';
/*Custom Routes*/
$route['dashboard/(:any)'] = 'root/dashboard/$1';
$route['search/(:any)'] = 'root/search/$1';
$route['search/(:any)/(:num)'] = 'root/search/$1/$2';
$route['export/(:any)'] = 'root/export/$1';
root controller
public function __construct()
{
parent::__construct();
$this->default_data = array("project_name" => PROJECT_NAME);
if ($this->router->method == "privacy_policy") {
return;
}
$this->load->library('session');
if ($this->router->method == "login") {
if ($this->session->userdata("username")) {
redirect(base_url("dashboard/"));
}
} else {
if (!$this->session->userdata("username")) {
redirect(base_url("login/"));
}
}
//Set MYSQL timezone
$this->db->query("SET time_zone = '+05:30'");
}
/**
* Dashboard View
*/
public function index()
{
redirect(base_url("/dashboard/"));
}
/**
* Login View
*/
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}
}
$this->load->view('login', $data);
}
Update
- 忘了说我只在远程服务器上遇到这个问题。在本地主机上它工作正常。
最佳答案
她的想法 - 在加载 session 对象后添加:
if ($this->session->userdata("username") && $this->router->method == "index") {
redirect(base_url("dashboard/"));
}
关于php - Codeigniter 3 索引方法行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56015312/