Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting get/post requests only from localhost

Because the data size isn't little that my web app needs to load, it gets pretty slow some times so therefor I decided to add some jQuery ajax functions to load certain data upon request and then save it in a cache.

What I would like to know is how can I limit any GET or POST requests only from localhost/same server/same ip so I can avoid any calls from outside to my app?

That means that my php functions that returns data, should return data only if requested from localhost.

My web app runs on CodeIgniter's framework and my web server's configuration is a LAMP running on ubuntu.

Any ideas?

like image 630
Alex Avatar asked Mar 26 '12 13:03

Alex


3 Answers

in the constructor you could use

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

However if this method isnt what you're looking for.. use .htaccess you can perform a quick google search to return a specific example for denying get/post to all and then allow for 127.0.0.1/localhost.

like image 102
NDBoost Avatar answered Oct 22 '22 01:10

NDBoost


Using .htaccess is probably the best way, allow only from your local address and 127.0.0.1. I found this example at petergasser.com and changed it only slightly:

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>  
like image 10
h00ligan Avatar answered Oct 22 '22 02:10

h00ligan


Use a key (think of API keys) to send along the request to your server. Then on your server you check that key and if it's the right one you return data.

like image 3
slash197 Avatar answered Oct 22 '22 02:10

slash197