开发者

AJAX query fails due to missing DEFINE variable in PHP

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 GET or POST, which you should check on PHP side. If you have received that variable, then define SOMETHING.

Here is your jQuery, using the POST method:

$.post('ajax.php', {SOMETHING: true}, function(ret){
    // do whatever you like with the return here
});

Here is your PHP, which will define SOMETHING if it receives a $_POST request with the variable SOMETHING in it.

<?php
if(isset($_POST['SOMETHING'])){
    define('SOMETHING', true);
}
if (!defined("SOMETHING")) { die("Error. You cannot access this file directly");}

// do anything you like here
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜