tags:

views:

117

answers:

3

Hi! I have a form on my site which sends data to some remote site - simple html form. What I want to do is to use data user enters into form for statistical purposes. So I instead of sending data to the remote page I send it first to my script which resends it the remote site.

The thing is I need it to behave in exact way the usual form would behave taking user to the remote site and displaying resources.

When I use this code it kinda works but not in the way I want it to:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $action);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);

Problem is that it displays response in the same script. For example if $action is for example: somesite.com/processform.php and my script name is mysqcript.php it would display the response of "somesite.com/processform.php" inside "mysqcript.php" so all the relative links are not working. How do I make it to send the user to "somesite.com/processform.php"? Same thing that pressing the button would do?

Leonti

+1  A: 

I think you will have to do this on your end, as translating relative paths is the client's job. It should be simple: Just take the base directory of the request you made

http://otherdomain.com/my/request/path.php

and add it in front of every outgoing link that does not begin with "/" or a protocol ("http://", "ftp://").

Detecting all the outgoing links is hard, but I am 100% sure there are ready-made PHP classes that do that. Check for example this article and the getLinks() function in the user comments. I am not 100% sure whether this is what you need but it certainly goes to the right direction.

Pekka
A: 

There are several ways to do that. Here's one of the easiest: just use a 307 redirect.

 header('Location: http://realsite.com/form_url.php', true, 307');

You can do your logging and stuff either before or after header() but if you do it after calling header() you will need to start your script with

ignore_user_abort(true);

Note that browsers are supposed to notify the user that their form is being redirected.

Josh Davis
+1  A: 

Here are a couple of possible solutions, which I post separately so they don't get mixed up with the one I recommend:

1 - keep using cURL, parse the response and add a <base/> tag to it. It should work for pretty much everything on that page.

<base href="http://realsite.com/form_url.php" />

2 - do not alter the submit URL. Submit the form to the real URL, but capture its content using some Javascript library (YUI does that) and send it to your script via XHR. It's still kind of hacky though.

Josh Davis