Why redirect function in CodeIgniter not working?

醉酒当歌 提交于 2019-12-12 02:55:43

问题


I have a really weird problem. I have been using CI for more than 2 years and it's the first time I found this kind of problem. I found that the redirect function is not working properly. I already load the url helper in autoload.php.

$autoload['helper'] = array('url','html','form','file');

What I want to do is to redirect the users to a controller if they call admin controller.

class Admin extends CI_Controller {
   function index(){
      redirect('secure');
   }
}

But what happen is the page redirected to the base_url. I've tried adding the second parameter of redirect with refresh or location but the result is just the same. It also happens to all redirect function in other controller. Can anyone tell me what the cause of this problem to happen?

EDIT: For additional information that might be helpful for finding the problem, here I put the code of the routes.php.

$route['default_controller'] = "frontpage";
$route['404_override'] = 'errors/page_missing';
$route['admin'] = 'secure';

回答1:


class Admin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    function index()
    {
         redirect('secure', 'refresh');
    }

}



回答2:


I just figured out that the problem is caused by a function in my helper. By coincident, I use site_url() function in the helper, which is already used by the CI. I replace that function with another name and now the redirection is working perfectly. I don't know how it can affects the redirect function, though. Thanks for anybody tried to help me.




回答3:


I also got this issue in my project and i found some white space at the bottom of my page after closing php tag.

There must be some white space or some kind of echo before your redirect() method or at the bottom of the page.

If this is not the issue then set log threshold to 4 in 'config/config.php' and check 'application/logs' folder.




回答4:


I had the same problem of redirect function's.

I found the solution by checking it's log file. Which is in \application\logs\

Then find the file which is addressed in log file and check if there is any space or any unused blank lines 'before and after the php tag "" '

If you find any space then remove it. And your redirect function would be work.

Thanks.




回答5:


it may be absence of index.php on url that is...., http://localhost/codigniter/index.php/controll_page/function_name so., redirect will be.....!!

redirect(base_url()."index.php/controll_page/function_name");



来源:https://stackoverflow.com/questions/20776247/why-redirect-function-in-codeigniter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!