php 为什么 CodeIgniter 中的重定向功能不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20776247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why redirect function in CodeIgniter not working?
提问by Abaij
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.
我有一个非常奇怪的问题。我用 CI 2 年多了,第一次发现这种问题。我发现重定向功能无法正常工作。我已经在 autoload.php 中加载了 url helper。
$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?
但是发生的情况是页面重定向到 base_url。我试过用刷新或位置添加重定向的第二个参数,但结果是一样的。它也发生在其他控制器中的所有重定向功能上。谁能告诉我这个问题发生的原因是什么?
EDIT: For additional information that might be helpful for finding the problem, here I put the code of the routes.php.
编辑:有关可能有助于查找问题的其他信息,我在这里放置了routes.php 的代码。
$route['default_controller'] = "frontpage";
$route['404_override'] = 'errors/page_missing';
$route['admin'] = 'secure';
回答by Mohammed Saqib Rajput
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function index()
{
redirect('secure', 'refresh');
}
}
回答by Abaij
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.
我刚刚发现问题是由我的助手中的一个函数引起的。巧合的是,我在 helper 中使用了 site_url() 函数,该函数已被 CI 使用。我用另一个名称替换了该函数,现在重定向工作正常。不过,我不知道它如何影响重定向功能。感谢任何试图帮助我的人。
回答by We'wake
I also got this issue in my project and i found some white space at the bottom of my page after closing php tag.
我在我的项目中也遇到了这个问题,我在关闭 php 标签后在我的页面底部发现了一些空白。
There must be some white space or some kind of echo before your redirect() method or at the bottom of the page.
在您的 redirect() 方法之前或页面底部必须有一些空白或某种回声。
If this is not the issue then set log threshold to 4 in 'config/config.php' and check 'application/logs' folder.
如果这不是问题,则在“config/config.php”中将日志阈值设置为 4 并检查“application/logs”文件夹。
回答by Nirav Thakar
I had the same problem of redirect function's.
我有同样的重定向功能问题。
I found the solution by checking it's log file. Which is in \application\logs\
我通过检查它的日志文件找到了解决方案。这是在 \application\logs\
Then find the file which is addressed in log file and check if there is any spaceor any unused blank lines'before and after the php tag "" '
然后找到在日志文件中寻址的文件,并检查在 php 标签前后是否有任何空格或任何未使用的空行“" '
If you find any space then remove it. And your redirect function would be work.
如果您发现任何空间,请将其删除。你的重定向功能会起作用。
Thanks.
谢谢。
回答by fasna
it may be absence of index.php on url that is...., http://localhost/codigniter/index.php/controll_page/function_nameso., redirect will be.....!!
可能是 url 上没有 index.php 是...., http://localhost/codigniter/index.php/controll_page/function_name所以., 重定向将是.....!!
redirect(base_url()."index.php/controll_page/function_name");
重定向(base_url()。“index.php/controll_page/function_name”);

