JOOMLA FICTION LAB
(5 votes)

How to block direct access to php file called by ajax function

Thursday, 22 March 2012 13:40
In php you can emulate an ajax request by inserting a proper header.

With this header you can prevent users from directly accessing php pages that are meant only for ajax calls.

It must be noted however that this is just a basic security step and it does not guarantee that a malicious user can not find a way to access the php file. This means that you should always make sure that your php code is secure.
// No direct access to this file
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(!IS_AJAX) {die('Restricted access');}




Add comment


Security code
Refresh