0

Im using cURL to post data to a php file (setcookie.php) on another domain.

The file, setcookie.php is supposed to set a cookie on that domain with the data posted to it.

The problem is the cookie doesn't want to be set when im doing it with cURL, because cURL returns to the calling file/domain, i guess.

So how can I make cURL not come back to the calling file?

Or is there an easier way to do this?

Here's my code :

$ch = curl_init ("http://<other domain>/setnewcookie.php");
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, false);
$returndata = curl_exec ($ch);
Andre J
  • 227
  • 4
  • 18
  • What are you trying to accomplish exactly? Get the cookie relayed to the client browser? – geekuality Jun 14 '11 at 19:50
  • I want to set a cookie on my other domain (domain2). The information that has to be set in that cookie comes from domain1's database. – Andre J Jun 14 '11 at 19:53
  • write ur cookie in a file and then set options like below :`curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");` also set `curlopt_returnontransfer` to `true` – xkeshav Jun 14 '11 at 19:56
  • What is the syntax for a cookie in a text file? – Andre J Jun 14 '11 at 20:05
  • See [http://www.cookiecentral.com/faq/#3.5](http://www.cookiecentral.com/faq/#3.5). – Francois Deschenes Jun 14 '11 at 20:08

2 Answers2

0

Here's what you need to do:

$ch = curl_init('http://example.org/setnewcookie.php');

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

For cookies to work with cURL, you need to define both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE. ALso, if you don't want the content of "http://example.org/setnewcookie.php" to be outputted to the browser, you need to set CURLOPT_RETURNTRANSFER to TRUE.

This will create a cookie on your server that cURL can use for subsequent requests but it won't allow the user of your website for instance to use that cookie. If the intent is for the user to be logged in on both sites, this will not work as-is.

For cross sub-domains (as in between www1.example.org and www2.example.org), have a look at PHP authentication with multiple domains and subdomains.

Community
  • 1
  • 1
Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • As soon as cURL submits the Post, I no longer care about this domain, I then want to be re-directed to the domain that sets the cookie. – Andre J Jun 14 '11 at 20:13
  • What exactly are you trying to do? Why would you want to create a cookie just to be redirected? First, cURL is server side so there's no need to be redirected. – Francois Deschenes Jun 14 '11 at 20:14
  • user details for a website are stored on my own domain. When a user updates his details, a new cookie needs to be set on the website. I know i can do this simply by submitting a html form, but can't curl do it too? – Andre J Jun 14 '11 at 20:18
  • Not really. If the "other domain" is in fact the same domain and you just want to set cookie, use [`setcookie()`](http://php.net/manual/en/function.setcookie.php). If the "other domain" is another domain, the only way that cookie will be sent to the user's web browser is if the user's web browser makes the request. You could instead generate a hidden image that links to an image on the other server, use an inline frame, an Ajax call but using cURL most likely won't work. – Francois Deschenes Jun 14 '11 at 20:21
  • Cool, thanks for the help. What I want to do is actually very simple, could just use an html form to submit the data, and then set the cookie, I just thought id give curl a try. – Andre J Jun 14 '11 at 20:27
  • cURL has plenty of uses but not that. You could use it to retrieve/send data from a web service (i.e. Twitter, Facebook, ...), retrieve other websites to include in your content, etc. It's really meant for PHP to make the requests. – Francois Deschenes Jun 14 '11 at 20:29
  • You could always consider Ajax if you don't have to have to submit the form. With an Ajax call, users that have JavaScript enabled wouldn't have to leave the page. Have a look at [http://api.jquery.com/jQuery.ajax/](http://api.jquery.com/jQuery.ajax/) to find out how to use jQuery (a JavaScript library) to do just that. – Francois Deschenes Jun 14 '11 at 20:31
0

If you want the cookie to get sent from domain2 to browser, browser needs to make request directly.

So if you must get the information from domain1 and user must not get it directly, I'd somehow encrypt the data and redirect browser to send the request to domain2 like this:

domain1/script.php

$return_url = 'http://domain1/script2.php';
$request_url = 'http://domain2/setnewcookie.php';
$request = $request_url . '?data=' . url_encode($encrypted_data) . '&return_url=' . urlencode($return_url);
header('Location: ' . $request);
exit;

And then in domain2/setnewcookie.php just decrypt the data, set the cookie and once that is done, redirect user back to domain1 with help of the $return_url.

Still not sure if this was what you were trying to accomplish, HTH.

geekuality
  • 355
  • 1
  • 5
  • All I want is to submit data to domain 2, completely leave domain1 and set a cookie on domain2 with the post data from domain1. – Andre J Jun 14 '11 at 20:14
  • So you are logging in or something? Then you can do it just as I suggested but forget about returning to domain1. If you absolutely need to POST to domain2 then You'll probably need a form that submits from domain1 to domain2. Either way it sounds that there is no use for cURL in your problem. – geekuality Jun 14 '11 at 20:20
  • I just find it very boring to create a form in html and submit it every time i want to post something. But thanks for the help, just thought i'd try different avenues... – Andre J Jun 14 '11 at 20:25