The action you requested is not allowed. Codeigniter

I am trying to make a simple login system using codeigniter. When I click on the login button, I get an error message:

The action you requested is not allowed.

When I open the console, I see this:

POST http://localhost/PHP/PROJECT/CodeIgniter/ 403 (forbidden)

This is my view:

 <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> 

This is my model:

 <?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(); } } 

and this is my controller:

 <?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"); } } 

How do I solve this problem?

+7
source share
4 answers

Check your config.php if,

 $config['csrf_protection'] = TRUE; 

If it is set to true, you need to use form_open() , this will automatically add ci_csrf_token . Otherwise, you can simply set to FALSE .

But it is advisable to set it to TRUE . But you must make sure that your entire request includes a ci_csrf_token including an AJAX request.

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

+10
source

try it

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

With me it was a browser cache , as soon as I cleared it, the form started working again.

0
source

For me, the problem was that I loaded the page into the index, I changed it as follows and worked:

 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'); } 
0
source

Source: https://habr.com/ru/post/985868/


All Articles