1
目前,只有当GET
请求发送到包含它的文件时,我才运行PHP函数。我通过JavaScript将这些请求发送到PHP文件。另外,如果get请求通过JavaScript文件发送并且不能直接访问,我希望它只运行该函数。只有在不直接访问PHP函数的情况下才能运行
例如:
run.js
function runfun(what, data) {
$.get("files/functions.php",{
run:'true',
what:what,
data:data
},
function(data) {
}
}
functions.php
if (isset($_GET['run']) && isset($_GET['what']) && isset($_GET['data'])) {
runstuff($_GET['what'], $_GET['data']); // I do NOT want this to run if
// the functions.php file is accessed directly and if the GET
// variables are set on the page itself
}
将一个唯一的令牌添加到在一定时间后过期的请求。你也可以检查请求是否来自AJAX,但它可能被欺骗。 – naththedeveloper
使用POST提交,您会将大多数“常规”用户从无法使用REST客户端的情况下直接调用它。 – h2ooooooo
你应该可以在['$ _SERVER'数组]中检查'REQUEST_URI'和/或'SCRIPT_FILENAME'(http://uk3.php.net/manual/en/reserved.variables.server.php) – SmokeyPHP