用PHP AJAX代理,这可能吗? [英] AJAX proxy with PHP, is it possible?

查看:137
本文介绍了用PHP AJAX代理,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的其他问题的参考:<一href="http://stackoverflow.com/questions/11790698/context-aware-ajax-call-in-a-modular-site">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

这篇关于用PHP AJAX代理,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆