tags:

views:

95

answers:

4

I am currently building a web app in which PHP files are loaded into a main file using jQuery's $.ajax function. However, the PHP files are obviously still accessible outside of the app, by just typing the files name in the address bar.

So my question is what would be the best way to make it so that the PHP file being 'ajaxed' in knows that it is contained in the correct page and will function correctly, but if it is accessed in any other way (even if someone were to make they're own website and AJAX in my PHP file) then the file should say "access denied" or something.

Thanks in advance

+6  A: 

Quoting Eran Galperin from a similar discussion

As others have said, Ajax request can be emulated be creating the proper headers. If you want to have a basic check to see if the request is an Ajax request you can use:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
 //Request identified as ajax request
}

However you should never base your security on this check. It will eliminate direct accesses to the page if that is what you need.

Please take this answer by Jeremy Ruten also into account:

There is no way of guaranteeing that they're accessing it through AJAX. Both direct access and AJAX access come from the client, so it can easily be faked.

Why do you want to do this anyways?

If it's because the PHP code isn't very secure, make the PHP code more secure. (For example, if your AJAX passes the user id to the PHP file, write code in the PHP file to make sure that is the correct user id.)

More clever thoughts in the discussion linked above.

middus
So how exactly is someone supposed to access this page with an XMLHttpRequest? Are you aware of the same origin policy?
Rook
I am aware of the same origin policy. The check for XMLHttpRequest can be made in order to find out whether the script hast been called via AJAX (i.e. your own script) or whether someone pointed his browser to this location. Calling the script from the Firbug console of your own site is a different story, though.
middus
A: 

It's not simple and propably there is no way to do that at all. You can require of some POST variables in your php file and send them in your AJAX request. This will secure you from someone who simply paste URL in his browser.

Nobody can AJAX your site from other domain (security issues), but always can connect and drieclty send http request, for example by cURL.

You can create some token in cookies, that will be also seen from jquery request, but that solution can also be hacked.

killer_PL
A: 

you could also block hotlinking if you are worried that others might access your content without your approval

see: http://www.htmlbasix.com/disablehotlinking.shtml

Christian Smorra
A: 

JavaScript running on another domain cannot access any page on your domain because this is a violation of the Same-Origin Policy. The attacker would need to exploit an XSS vulnerability in order to pull this off. In short you don't need to worry about this specific attack, just the same old attacks that affect every web application.

Rook