代码点火器的htaccess和POST变量问题


htaccess and POST variable issues with Codeigniter

所以我正在对这个话题做一些研究,你会认为会有一个答案,但没有,或者我找错了地方。

问题

提交表单时,我的帖子变量为空。我正确地传递它们,表单去它需要去的地方,变量和表单数据在标题中正确显示并带有值。我在配置中自动加载了表单助手,所以一切都是应该的。

我的表单打开器如下所示:

<form action='/Forms/form_processor/' method='post' id='testForm'>

在我的Forms控制器看起来像这样:

define('BASEPATH') OR exit('No direct script access allowed');
class Forms extends CI_Controller{
    public function form_processor(){
        // get variables
        $name = $this->input->post('name');
        // ... do stuff with variable data
    }
}

我还要指出,通过使用 htaccess 文件,我的配置变量是:

$config['base_url'] = 'http://example.com/';
$config['index_page'] = ''; // this is to keep index.php out of the URL

可能的原因

当使用代码点火器并坐在主页上时,你会得到这个URL:

http://example.com

这很好,直到您转到下一页,您才会得到此 URL:

http://example.com/index.php/page

它看起来很丑,所以出于美学目的并保持统一性,我创建了一个htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /example.com/
    RewriteCond $1 !^(index'.php|images|robots'.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 http://example.com/
</IfModule>

然后通过在基目录中放置此 htaccess 文件,我得到的 URL 如下所示:

http://example.com/page

临时修复和需要回答的问题

有人向我指出,因为我的$config['index_page']变量为空,所以这就是问题的根源。因此,我在表单操作中将index.php添加到URL的开头,然后它通过了。

htaccess 重写不应该处理这个问题,并将其发送到它应该去的地方,我是否在表单的操作 URL 中有index.php

其次,这将如何影响帖子变量?因为当我提交表单时,它仍然到达需要去的地方,它只是为所有变量显示 null,即使它们通过标头正确发送。这是我最困惑的地方。

如果您需要更多信息,请告诉我,但我们只是想弄清楚为什么会发生这种情况,这有点尴尬。提前谢谢你!

你的问题似乎很复杂。我认为安装步骤可能存在问题。从配置中删除索引.php,设置基本URL(也在应用程序/config.php中),设置加密密钥(也在那里)。

检查会话设置是什么 - 您是否在那里更改了某些内容。

也简化htaccess(把它放在你的索引.php)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

这是本地问题还是服务器问题?

最后 - 尝试使用表单助手,它是否改变了一些东西,控制台内部有什么?显示控制器的更多内容。您可以在一个功能中查看帖子

class Forms extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    function form_processor()
    {
        //check if $_POST
        if ($this->input->post()) {
            $name=$this->input->post('name');
            //process and redirect
        }
        $this->load->view('forms/my_form');
    }
}

此外,如果您使用 xss_filtering 或 csrf_protection(在 config.php 中检查),您绝对应该使用表单助手。

/forms/form_processor/' method='post' id='testForm'>

在表单操作中使用基本 URL。如果不起作用,请在基本 URL 后使用 index.php。