2

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).

2 Answers 2

1

/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

Sign up to request clarification or add additional context in comments.

3 Comments

So header redirection works also for AJAX calls? That seems interesting, adapting to include POST headers should not be that difficult...
I'm afraid it is difficult. AJAX requests are in essence standard HTTP requests, so no magic here. What we do in this PHP code is we send redirect header, which goes back to the browser which in turn asks given URL for content. But it does not send POST data second time (it's normal POST-REDIRECT-GET pattern). To be able to send POST data, you'll have to send POST data and read response inside PHP script (using cURL perhaps) and output the response. Google 'php curl post' for code samples.
Yep, based on your answer I've already done a bit of research and cURL seems the way to go. Or, maybe, making a GET request in which POST variables are specially marked and transformed into GET variables (but if I'm not wrong GET has length limits, so there would be not a complete compatibility). Thanks for your explanation :)
0

Instead you can also declare your javascript variable in your first page request and then use it let say SITE_URL contains your php computed url

 var siteRoot = '<?php echo SITE_ROOT?>' // or some thing else 

so for any .js script included after this line will have defined siteRoot var.

1 Comment

As stated in my other question (the one I've linked to), I am already using that approach, but I want some more flexibility in the URL. I am asking anyway about a real AJAX proxy script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.