用PHP AJAX代理,这可能吗?这可、PHP、AJAX

由网友(  ≯家里我主外≮ ≮家里我主内≯)分享简介:在我的其他问题的参考:Context-aware模块化网站 AJAX调用,是否有可能建立一个AJAX代理以便捷的方式?In reference of my other question: Context-aware AJAX call in a modular site , is it possible to set...

在我的其他问题的参考:Context-aware模块化网站 AJAX调用,是否有可能建立一个AJAX代理以便捷的方式?

In reference of my other question: Context-aware AJAX call in a modular site , is it possible to set up an AJAX proxy in a convenient way?

我希望做动态的URL AJAX请求没有污点JavaScript的code与服务器端的PHP指令,计算正确的路径,以模块化的Apache / PHP / MySQL的网站阿贾克斯PHP文件,粗略CMS我写的。 换言之,网站具有包含其的CSS,JS和PHP插件和组件用自己的文件夹结构和我有功能动态检索其文件夹。我希望:

I'd wish to make AJAX requests with dynamic url without tainting the JavaScript code with server-side PHP instructions, computing the right path for the ajax php file in a modular Apache/PHP/MySQL site, a rough CMS written by me. In other words, the site has plugins and components with their own folder structures containing their CSS, JS, and PHP and I have functions to retrieve their folder dynamically. I'd wish that:

$("#mydiv").load(siteRoot + "/ajax.php?plugin=datagrid&
action=myaction&... other params...");

将改为调用(与计算的URL服务器端用PHP):

will instead call (with the URL computed server-side by PHP):

{siteRoot}/components/datagrid/ajax/myaction.php?... other params ...

(很明显,对注射剂,CSRF及其他黑客所有可能的检查)。

(obviously with all the possible checks against injections, CSRF and other hacks).

推荐答案

/ajax/get.php文件看起来应该是这样的:

/ajax/get.php file should look something like this:

$plugin = $_GET['plugin'];
$action = $_GET['action'];
$get = array();
foreach ($_GET as $k=>$v) {
    if($k!='plugin' && $k!='action') {
        $get[] = "{$k}={$v}";
    }
}
$url = 'siteRoot/components/'.$plugin.'/ajax/'.$action.'.php?'.implode('&', $get);

header("Location: {$url}");

当然,你需要一些安全检查添加到上面的code

Of course, you need to add some security checks to above code

编辑:缺点是,它不会发送POST请求的工作

The downside is that it won't work for sending POST requests

阅读全文

相关推荐

最新文章