【问题标题】:The action you have requested is not allowed. Codeigniter您请求的操作是不允许的。代码点火器
【发布时间】:2023-03-10 05:45:02
【问题描述】:

我正在尝试在 codeigniter 中创建一个简单的登录系统。当我点击我的按钮登录时,我收到一个错误:

您请求的操作不被允许。

当我打开控制台时,我看到:

POST http://localhost/PHP/PROJECT/CodeIgniter/403(禁止)

这是我的看法:

<body>
    <h1>LOG IN!</h1>
    <form action="" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" >
        <label for="password">Password</label>
        <input type="password" id="password" name="password" >
        <br>
        <button id="btn_login" name="btn_login" >LOG IN!</button>
    </form>
    <div class="errors" ><?php echo validation_errors(); ?></div>
</body>

这是我的模型:

<?php 
class User_model extends CI_Model {
    public $m_sUsername;
    public $m_sPassword;
    public $m_sEmail;
    public $m_sPicture;

    function __construct()
    {
        parent::__construct();
    }

    function get_user($username, $password)
    {
        $this->db->select("username","password");
        $this->db->from(user);
        $this->db->where('username',$username);
        $this->db->where('password',$password);
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->num_rows();
    }
}

这是我的控制器:

<?php

class Login extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->database();
        $this->load->library('form_validation');
        $this->load->model("User_model", "", true);
    }

    public function index()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $username = $this->input->post("username");
            $password = $this->input->post("password");
            $this->form_validation->set_rules("username", "Username", "trim|required");
            $this->form_validation->set_rules("password", "Password", "trim|required");

            if ($this->form_validation->run() == FALSE) {
                //validation fails
                echo "Vul alle velden in";
            } else {
                //validation succeeds
                if ($this->input->post('btn_login') == "Login") {
                    //check if username and password is correct
                    $usr_result = $this->User_model->get_user($username, $password);
                    if ($usr_result > 0) { //active user record is present
                        echo 'Ingelogd!';
                    } else {
                        echo "Wrong!";
                    }
                }
            }
        }

        $this->load->view("admin/login_view.php");
    }
}

我该如何解决这个问题?

【问题讨论】:

  • 在表单中有按钮类型的按钮不是一个好习惯。尝试使用按钮类型提交或输入类型提交

标签: php codeigniter authentication post button


【解决方案1】:

检查你的config.php如果,

$config['csrf_protection'] = TRUE;

如果设置为true,您需要使用form_open(),这将自动附加ci_csrf_token。否则你可以设置为FALSE

但建议将其设置为TRUE。但是您需要确保您的所有请求都包含ci_csrf_token,包括 AJAX 请求。

https://www.codeigniter.com/user_guide/helpers/form_helper.html

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • +1 除了选择的答案,如果开发人员不使用form_open(),这也是一个方便的解决方案
    【解决方案3】:

    对我来说是浏览器缓存,一旦我清除它,表单又开始工作了。

    【讨论】:

    • 我现在遇到同样的错误。
    【解决方案4】:

    对我来说,问题是我正在将页面加载到索引中,我进行了如下更改并且它起作用了:

    public function index()
    {
        // Load Login Page
        redirect('login/login_page','refresh');
    
    }
    
    public function login_page()
    {
        $data['title'] = 'Login Page';
    
        $this->load->view('templates/header', $data);
        $this->load->view('users/login_view', $data);
        $this->load->view('templates/footer');
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 2017-05-14
      • 2012-05-10
      • 2016-04-03
      • 2014-02-08
      • 2016-07-25
      • 2019-09-13
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多