2013-07-13 35 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(); 
}