Reputation: 9
my problem is sending variables between two php files
i know there is session for these stuff but i don't wanna use session for this... i want to have something as it POSTs in html forms..i used cURL like this between two files
ss.php
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"http://localhost/dd.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat");
curl_exec ($curl);
curl_close ($curl);
?>
and there is second file called dd.php
<?php
echo $_POST['Hello'];
?>
it works fine but the problem is it doesn't redirect to dd.php file... like it does in forms in html files
how can i get it redirected to the second file?
Upvotes: 0
Views: 865
Reputation: 219077
You seem to be misunderstanding how web requests work, so let's take a step back...
When a user goes from one page to another, the server isn't making this happen. The client is. The user's browser is displaying a page, then it makes a request for another page. This is also true of redirects. In the case of a redirect, the server is responding with the redirect address and the client is taking that information and making another request.
The order of steps is like this:
At no point does Page 2 send anything to Page 3. So unless you persist the data somewhere that both pages can access (session, database, etc.) then you need the user to send the data. In this particular scenario, that would have to be as query string values in the redirect. (Since a redirect results in a GET request.) Something like this:
header('Location: dd.php?Hello=World&Foo=Bar&Baz=Wombat');
This would redirect the user like any other redirect, and the URL simply contains the values you want the user to carry to the next page.
If it's data you don't want the user to see, or is a significant amount of data, then you'll want to fall back to some server-side persistence (session, database, etc.) instead of routing the data through the request itself.
Alternatively, you could make the cURL request as you do now and then perform the redirect. But I highly doubt that's what you want, since that would be making two requests to the target page. One made by the server to post the data, and another entirely separate one made by the client to navigate to the page. And those requests would have nothing to do with one another and wouldn't share data.
Upvotes: 0
Reputation: 706
You need to add the following to redirect to the dd.php
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Upvotes: 1