tags:

views:

717

answers:

6

I'm calling the php code from ajax like this:

ajaxRequest.open("GET", "func.php" + queryString, true);

Since it's a get request anyone can see it by simply examining the headers. The data being passed is not sensitive, but it could potentially be abused since it is also trivial to get the parameter names.

How do I prevent direct access to http://mysite/func.php yet allow my ajax page access to it?

Also I have tried the solution posted here but its doesn't work for me - always get the 'Direct access not premitted' message. Thanks-

+1  A: 

Mmm... you could generate a one-time password on session start, which you could store in the _SESSION, and add a parameter to your ajax call which would re-transmit this (something like a captcha). It would be valid for that session only.

This would shield you from automated attacks, but a human who has access to your site could still do this manually, but it could be the base to devise something more complicated.

Palantir
Thanks - this seems like overkill for my scenario. The page I call is just sending an email - the parameter passed is the email address. I really only asked this question because Im still pretty new to ajax, and this seems like something that may be an issue in future applications.
jriggs
A: 

Based on your description, I assume you're trying to prevent outright rampant abuse, but don't need a rock-solid solution.

From that, I would suggest using cookies:

Just setcookie() on the page that is using the AJAX, and check $_COOKIE for the correct values on func.php. This will give you some reasonable assurance that anyone calling func.php has visited your site recently.

If you want to get fancier, you could set and verify unique session ids (you might do this already) for assurance that the cookie isn't being forged or abused.

anschauung
+1  A: 

what I use is: PHP sessions + a hash that is sent each time I do a request. This hash is generated using some algorithm in the server side

Gabriel Sosa
+1  A: 

Wouldn't be the case to block GET requests on the file and only accepting POSTS coming from the same server?

F.Aquino
+5  A: 

Most Ajax requests/frameworks should set this particular header that you can use to filter Ajax v Non-ajax requests. I use this to help determine response type (json/html) in plenty of projects:

if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) )
{
 // allow access....
} else {
 // ignore....
}

edit: You can add this yourself in your own Ajax requests with the following in your javascript code:

var xhrobj = new XMLHttpRequest();
xhrobj.setRequestHeader("X-Requested-With", "XMLHttpRequest");
Ian Van Ness
Although not 100% effective (in the case headers are spoofed), this is a very quick and easy solution to only allow AJAX calls to the page. +1
cballou
Ah...this is the type of solution I expected to get. Unfortunately, I'm not using any frameworks - just straight javascript/ajax. Any rate, the variable isn't set with my code so this doesn't work for me.
jriggs
A: 

I would question why you are so convinced that no-one should be able to visit that file directly. Your first action really should be to assume that people may visit the page directly and act around this eventuality. If you are still convinced you want to close access to this file then you should know that you cannot trust $_SERVER variables for this as the origins of $_SERVER can be difficult to determine and the values of the headers can be spoofed. In some testing i did i found those headers ($_SERVER['HTTP_X_REQUESTED_WITH'] & $_SERVER['HTTP_X_REQUESTED_WITH']) to be unreliable as well.

seengee