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?
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...");
will instead call (with the URL computed server-side by PHP):
{siteRoot}/components/datagrid/ajax/myaction.php?... other params ...
(obviously with all the possible checks against injections, CSRF and other hacks).
/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}");
Of course, you need to add some security checks to above code
EDIT: The downside is that it won’t work for sending POST requests