【问题标题】:Cannot perform POST request in CodeIgniter无法在 CodeIgniter 中执行 POST 请求
【发布时间】:2015-11-28 22:38:11
【问题描述】:

我在页面中有一个表单,它使用 CodeIgniter 框架将用户数据上传到我的在线服务器。我可以使用 HTTPS 协议正常访问表单页面,但是当我尝试使用 POST 提交表单时,出现403 Forbidden: You don't have permission to access <url> on this server. 错误。可能是什么问题?

.htaccess 文件:

RewriteEngine On
RewriteCond $1 !^(index\.php|resources|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

我在视图中的表单属性。 $base_url 变量是在config.php 中配置的base_url()

<form name="new_article_form" id="new_article_form" method="POST" action="<?php echo $base_url; ?>entries/insert_article" enctype="application/x-www-form-urlencoded">
...
</form>

我错过了什么吗?

编辑: 按要求填写完整表格。它现在不使用 CI 的 form_helper。目前正在努力:

<form name="new_article_form" id="new_article_form" method="POST" action="<?php echo $base_url; ?>entries/insert_article" enctype="application/x-www-form-urlencoded">

    <p>Blog Name:</p>
    <select name="blog" id="blog" required>
    <?php
    echo $blogs; //pre-formatted <option> list from controller
    ?>
    </select>
    <p>Article Title:</p>
    <input type="text" name="title" id="title" required />

    <p>Article Author:</p>
    <input type="text" name="author" id="author" value="Own" required />

    <p>Authoring Date:</p>
    <input type="datetime-local" name="auth_date" id="auth_date" value="<?php echo date("d-m-Y"); ?>" required />

    <p>Article URL:</p>
    <input type="text" name="url" id="url" required />
    <p>Summary:</p>
    <textarea name="summary" id="summary" style="resize:none" class="text" rows="10" cols="40" required ></textarea>
    <br />
    <br />
    <button name="reset" id="reset" type="reset">Clear</button>&nbsp;
    <button name="submit" id="submit" type="submit">ADD</button>
</form>

更新:

我刚刚使用 CI 的表单助手库重写了整个表单,但仍然遇到同样的错误。表单请求甚至没有到达控制器或主 index.php 文件,在此之前它被终止,因为没有 POST 数据到达框架根目录的主 index.php 文件。因此,这可能是权限问题或在此之前的其他问题。注意:我在一个共享的网站托管平台上,以防万一有人想知道,并且目前正在使用自签名证书进行在线 SSL 测试。

更新 2:

routes.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['default_controller'] = "main";
$route['404_override'] = '';

/* End of file routes.php */
/* Location: ./application/config/routes.php */

config.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['base_url'] = 'https://<***full_url***>/';
$config['index_page'] = '';

$config['uri_protocol'] = 'AUTO';

$config['url_suffix'] = '';
$config['language'] = 'english';
$config['charset'] = 'UTF-8';
$config['enable_hooks'] = FALSE;
$config['subclass_prefix'] = 'BG_';
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = FALSE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['directory_trigger']    = 'd'; // experimental not currently in use

$config['log_threshold'] = 2;
$config['log_path'] = '';
$config['log_date_format'] = 'Y-m-d H:i:s';
$config['cache_path'] = '';
$config['encryption_key'] = '';
$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;
$config['global_xss_filtering'] = FALSE;

$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = '************';
$config['csrf_cookie_name'] = '*************';
$config['csrf_expire'] = 3600;
$config['compress_output'] = FALSE;
$config['time_reference'] = 'local';

$config['rewrite_short_tags'] = FALSE;

$config['proxy_ips'] = '';

/* End of file config.php */
/* Location: ./application/config/config.php */

条目控制器:

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Entries extends CI_Controller 
{
public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model("entries_model", "entries");
    }

    public function index($data = array())
    {

        ...//other code

        $form_attrs = array("name"=>"new_article_form", "id"=>"new_article_form", "enctype"=>"application/x-www-form-urlencoded");

        $form = form_open(base_url()."entries/insert_article", $form_attrs);

        //... form entries

        $form .= form_close();

        $data["form"] = $form;

        $this->load->view('entries_view', $data); //the form is displayed on the view properly, no errors present, exactly as shown above
    }

    public function insert_article()
    {
        $blogID = $this->input->post("blog");
        $title = mb_convert_encoding($this->input->post("title"), "UTF-8");

        //... other insert code

        $this->index($data);
    }

    public function blogs($result = "")
    {
        //... blogs view function
        $this->load->view('blogs_view', $data);
    }
}

    //... other code
?>

注意:据我所知,错误出在服务器端,因为来自使用该站点的移动应用程序的 POST 请求能够通过,但浏览器 POST 请求特别是带有表单数据的请求没有通过。 GET 请求虽然顺利通过。唯一可见的错误也在服务器错误日志中,指出“404.html”页面无法找到。任何人都知道什么可能会拒绝服务器端的连接?

更新:

HTTP 标头:

Host: <***site***>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101     Firefox/40.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://<***site***>/entries/blogs
Cookie: PHPSESSID=ba4ce8f6cf3ebd19a443763fa8a187c0
Connection: keep-alive

【问题讨论】:

  • 打开您的开发者工具并检查网络选项卡。我认为你的 POST 工作正常。
  • @GuyT 我这样做了,但它显示了 403 禁止错误
  • @Peter 你能发布生成的 HTML 表单吗?
  • 我想问一下,我刚刚从标头输出和一些研究,我的托管服务器不支持这些类型
  • 导航到 host/index.php/entries/insert_article 并在 insert_article 函数的第一行插入一个回显。您使用的是 .htaccess 吗?

标签: php .htaccess codeigniter


【解决方案1】:

在您的表单操作中使用 site_url 助手,然后检查您的控制器是否正在发布。

<form name="new_article_form" id="new_article_form" method="POST" action="<?php echo site_url('entries/insert_article') ?>" enctype="application/x-www-form-urlencoded">

也别忘了包含 url helper。

【讨论】:

  • 使用site_url() 没有帮助,仍然出现同样的错误
  • 我的浏览器没有错误跟踪,只有错误消息,我的服务器访问日志显示所有访问权限都被授予,我也找不到 CI 日志上的错误堆栈跟踪
  • @Peter 您是否检查过其他帖子请求是否有效?
  • 我刚刚尝试了对同一个控制器的另一个 POST 请求,但使用了不同的方法/功能和不同的视图:相同的技术,使用表单助手,并且它有效。然后我切换标签再次尝试上面的页面,它失败了。现在不确定这里到底发生了什么。
  • @Peter 你有没有添加任何自定义路线
【解决方案2】:

事实证明,表单提交的数据被检测为对服务器 Mod 安全防火墙的 XSS 攻击,这就是为什么在可以加载 CI 之前请求就被拒绝的原因。它现在已在我的域中被禁用,现在提交工作。希望从现在起不再有任何障碍。

【讨论】:

  • 请问为什么服务器将请求视为 XSS 攻击?您没有正确编码数据吗?
  • @GuyT 也不确定,因为表单数据是通过 POST 请求提交的简单 UTF-8 编码文本,没什么特别的。从我所看到的情况来看,有一些特殊字符正在被检查,它们会触发误报,例如我发布的数据中的分号和冒号
猜你喜欢
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 2018-04-25
  • 1970-01-01
  • 2021-06-25
相关资源
最近更新 更多