2013-07-13 69 views
0

我正在使用Codeigniter。當用戶提交填寫所有詳細信息的表單時,我有以下方法 - create_client。如何防止在Codeigniter中通過GET訪問POST方法?

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

class Admin extends CI_Controller { 

public function create_client() 
{ 

// catch all the form data here 
//process form data 


} 

} 

該函數是爲接受表單提交而設計的。但是,如果有人試圖訪問admin/create_client(GET),他也可以直接執行該函數。由於沒有通過GET語句的表單數據,這會導致錯誤。

如何防止通過GET訪問的方法。一個解決方案是把一些檢查的方法 -

if ($_SERVER['REQUEST_METHOD'] === 'POST') { 

// do things here 

} else { 

return false; 

} 

但是我有很多這樣的方法,我不想改變所有這些方法。有沒有簡單的方法?比如說在路由配置中指定這個方法是一個POST函數,並且不能通過GET訪問?

+1

你可能想看看[菲爾鱘魚的CI REST風格的服務器實現(https://github.com/philsturgeon/codeigniter-restserver),這裏是一個[有益教程](http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/)。 –

回答

0

嘗試remap

public function _remap($method, $params = array()) 
{ 
    switch($method) { 
     case 'post_method': 
     case 'post_method2': 
      if (! $_SERVER['REQUEST_METHOD'] === 'POST') 
       // return error 
    } 
    if (method_exists($this, $method)) 
     return call_user_func_array(array($this, $method), $params); 
    show_404(); 
}