I am new to AJAX/jQuery with PHP.
I am trying to call a PHP script via AJAX using XMLHttpRequest or jQuery. In both cases the call fails because the php file I am calling into contains on the very first row the following check
if (!defined("SOMETHING")) { die("Error. You cannot access this file directly");}
which causes that my call fails, because this variable is not defined in this case as I am calling from the outside. This condition just checks if the caller is the same web application or wheather the call comes from outside (then it will be denied).
Is there a workaround for it without removing this check? Can I somehow set this expected variable using AJAX/jQuery?
Is there a way how to call specific PHP method via AJAX without calling into the whole PHP file?
Thanks in advance
Tomas
Hmm… you can do it, but I am not sure if this is secure or the way you would like it to be.
Your jQuery should post a variable with
GETorPOST, which you should check on PHP side. If you have received that variable, then defineSOMETHING.Here is your jQuery, using the
POSTmethod:Here is your PHP, which will define
SOMETHINGif it receives a$_POSTrequest with the variableSOMETHINGin it.