2015-04-24 14 views
6

Tôi đang cố gắng tạo một hệ thống đăng nhập đơn giản trong trình mã hóa. Khi tôi bấm vào nút đăng nhập của tôi, tôi nhận được một lỗi:Hành động bạn yêu cầu không được phép. Codigniter

The action you have requested is not allowed.

Khi tôi mở giao diện điều khiển của tôi, tôi thấy điều này:

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

này được quan điểm của tôi:

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

Đây là của tôi mô hình:

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

và đây là bộ điều khiển của tôi:

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

Có ai biết cách giải quyết vấn đề này không?

Cảm ơn!

+0

không phải là một thực hành tốt để có một nút nút gõ vào một biểu mẫu. Hãy thử với loại nút gửi hoặc loại đầu vào gửi –

Trả lời

9

Kiểm tra của bạn config.php Nếu,

$config['csrf_protection'] = TRUE; 

Nếu nó được thiết lập là true bạn cần phải sử dụng form_open(), điều này sẽ tự động thêm các ci_csrf_token. Nếu không, bạn chỉ có thể đặt thành FALSE.

Nhưng được khuyên nên đặt thành TRUE. Nhưng bạn cần đảm bảo tất cả yêu cầu của bạn bao gồm ci_csrf_token bao gồm yêu cầu AJAX.

https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

+1

Cảm ơn bạn đã giúp tôi! Nó đã làm việc. –

9

Hãy thử điều này

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

+1 bên cạnh ** câu trả lời được chọn ** đây cũng là giải pháp hữu ích nếu nhà phát triển không sử dụng 'form_open()' – Adnan

-1

Với tôi nó là trình duyệt bộ nhớ cache, một khi tôi xóa nó, hình thức bắt đầu hoạt động trở lại.

0

cho tôi vấn đề là tôi đã được tải trang trong chỉ mục, tôi đã thay đổi như sau và nó làm việc:

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'); 
} 
Các vấn đề liên quan