不允许您请求的操作。即使没有csrf保护

问题描述 投票:0回答:2

在我的codeigniter项目中.CSRF保护未设置为true。但是当我提交表单时,我收到以下错误“遇到错误您不允许请求的操作。”。这是我的表格视图

<?php echo form_open('business/add-user',array('class'=>'form-horizontal'));?>
                                <div class="form-group">

                    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">

                                    <label class="col-md-2 control-label">User Name<span

                                                class="text-danger">*</span></label>

                                    <div class="col-md-7">
                                        <input type="hidden" name="business_id" 
                                        value="<?php echo $business_id;?>">
                                        <input class="form-control" type="text" id="name"

                                               value="<?php echo set_value('name'); ?>" name="name"

                                               placeholder="User Name">

                                        <div style="margin-top: 0px; color: red;"><?= form_error('name'); ?></div>

                                    </div>

                                </div>

                                <div class="form-group">

                                    <label class="col-md-2 control-label">Email Address<span

                                                class="text-danger">*</span></label>

                                    <div class="col-md-7">

                                        <input class="form-control" type="email" id="email"

                                               value="<?php echo set_value('email'); ?>" name="email"

                                               placeholder="Email Address">

                                        <div style="margin-top: 0px; color: red;"><?= form_error('email'); ?></div>

                                    </div>

                                </div>

                                <div class="form-group">

                                    <label class="col-md-2 control-label">Phone<span class="text-danger">*</span></label>

                                    <div class="col-md-7">

                                        <input class="form-control" type="number" id="phone"

                                               value="<?php echo set_value('phone'); ?>" name="phone"

                                               placeholder="Phone">

                                        <div style="margin-top: 0px; color: red;"><?= form_error('phone'); ?></div>

                                    </div>

                                </div>

                                <div class="form-group">

                                    <label class="col-md-2 control-label">Password<span class="text-danger">*</span></label>

                                    <div class="col-md-7">

                                        <input class="form-control" type="password" id="password"

                                               value="<?php echo set_value('password'); ?>" name="password"

                                               placeholder="Password">

                                        <div style="margin-top: 0px; color: red;"><?= form_error('password'); ?></div>

                                    </div>

                                </div>
                                <div class="form-group">

                                    <div class="col-md-8 col-md-offset-2">

                                        <button class="btn btn-sm btn-primary" name="submit" type="submit"><i

                                                    class="fa fa-check"></i> Add User

                                        </button>

                                    </div>

                                </div>

                            <?php echo form_close();?>

我的控制器

        public function add_user()
{
    $this->form_validation->set_rules('name','Name','required|xss_clean');
    $this->form_validation->set_rules('email','Email','required|xss_clean|valid_email|is_unique[user_login.email]');
    $this->form_validation->set_rules('phone','Phone','required|xss_clean|is_unique[user_login.phone]');
    $this->form_validation->set_rules('password','Password','required|xss_clean');
    $this->form_validation->set_error_delimiters('','');
    if($this->form_validation->run()===FALSE)
    {
        $data['business_id']=$this->input->post('business_id');
        $this->load->view('admin/business/create_user',$data);
    }

    else
    {
        if($this->Business_Model->add_user())
        {
            redirect('account/business-profile/'.$this->input->post('business_id'));
        }

        else
        {
            $this->session->set_flashdata('SUCCESSMSG','User not created');
            $data['business_id']=$this->input->post('business_id');
            $this->load->view('admin/business/create_user',$data);
        }
    }
}

形式为$ route ['business / add-user'] ='admin / Business / add-user'的形式的行动;

即表单操作是控制器中的add_user我需要克服这个问题

codeigniter csrf
2个回答
0
投票

尝试删除<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">我不是100%肯定,如果这是导致问题。

form_open已经为你添加了一个隐藏字段和csrf令牌!


0
投票

我可以解决它。问题是我给出的表单是business / add-user的行为。这个路由在routes.php文件中配置。在xampp中没有问题。但是在我提交表单的服务器中“你要求的动作不允许“出现错误。所以我将表单操作改为默认路由”admin / Business / add-user“。纠正错误

© www.soinside.com 2019 - 2024. All rights reserved.