仅接受来自localhost的get/post请求

Ale*_*lex 17 php ajax post codeigniter get

因为我的Web应用程序需要加载的数据大小不小,所以它会变得非常缓慢,所以我决定添加一些jQuery ajax函数来根据请求加载某些数据然后将其保存在缓存中.

我想知道的是如何限制来自localhost /同一服务器/同一个ip的任何GETPOST请求,以便我可以避免从外部到我的应用程序的任何调用?

这意味着我的PHP函数返回数据,只有在从localhost请求时才返回数据.

我的Web应用程序在CodeIgniter的框架上运行,我的Web服务器的配置是在ubuntu上运行的LAMP.

有任何想法吗?

gor*_*ive 26

在你可以使用的构造函数中

if ($_SERVER['SERVER_ADDR'] != $_SERVER['REMOTE_ADDR']){
  $this->output->set_status_header(400, 'No Remote Access Allowed');
  exit; //just for good measure
}
Run Code Online (Sandbox Code Playgroud)

但是,如果这种方法不是您正在寻找的...使用,.htaccess您可以执行快速谷歌搜索以返回拒绝获取/发布到所有的特定示例,然后允许127.0.0.1/localhost.

  • 注意:您可以伪造remote_addr http://stackoverflow.com/questions/5092563/how-to-fake-serverremote-addr-variable (3认同)

h00*_*gan 10

使用.htaccess可能是最好的方法,只允许使用本地地址和127.0.0.1.我在petergasser.com上找到了这个例子并且只是稍微改了一下:

AuthName "bla"  
AuthType Basic  
<Limit GET POST>  
order deny,allow  
deny from all 
allow from 127.0.0.1
allow from <your-ip-here>
</Limit>  
Run Code Online (Sandbox Code Playgroud)