Jump to content

[PHP 5.2.8] Retrieve cookies from remote host and pass them back to client.


jasonc2

Recommended Posts

I have a PHP 5.2.8 web application that needs to retrieve a resource from a remote URL. However, the remote host also sends a cookie back (in a Set-Cookie header), and I need to grab that cookie and pass it on to the client (so that it's stored by the client browser). I do know the name of the cookie beforehand.

 

What is the best way to do this? Passing a cookie from PHP back to the client is easy, it's grabbing the cookie from the remote URL that is the problem.

 

There is no support for cookies in file_get_contents(), so that won't work. I can use curl to retrieve the remote URL with headers, but curl does not seem to provide an easy way to get the value of the cookie beyond retrieving the HTTP headers and parsing them manually. I could use and parse curl's cookie file (another hack) but the cookie contains a value that is randomly generated, and the cookies in curl's cookie jar are global to all PHP requests (so if multiple users made the same request, their cookie value could be shared).

 

How can I retrieve the contents of a remote resource as well as the value of a cookie sent with that resource?

 

Thanks,

Jason

 

P.S.: Also, as a minor side question; how, if at all, would the method for doing this be different in PHP 4.4.9?

Link to comment
Share on other sites

Somewhere in the header of that output should be "Set-Cookie:" if it worked correctly.

 

(Not tested) - change the URL Below:

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, TRUE);

// grab URL and pass it to the browser
$opt = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

echo '<pre>';
echo htmlentities($opt);
echo '<pre>';

Link to comment
Share on other sites

Thanks for the replies.

 

So you have a PHP application that needs to contact a remote server, get a specific cookie, then take that value and pass it to YOUR local client?

 

what is your local client?

 

Yep. The local client is a web browser. The reason I need to do that is the cookie obtained by the request made from the PHP application also needs to be sent with AJAX-style requests from the client browser directly to the remote server, so the cookie needs to be stored in the browser's cookie store.

 

However, I've found a solution. I've been able to get the whole thing working using HttpRequest from the PECL extensions. It contains methods for easy access to cookies received from requests.

 

Thanks,

J

Link to comment
Share on other sites

Thanks for the replies.

 

So you have a PHP application that needs to contact a remote server, get a specific cookie, then take that value and pass it to YOUR local client?

 

what is your local client?

 

Yep. The local client is a web browser. The reason I need to do that is the cookie obtained by the request made from the PHP application also needs to be sent with AJAX-style requests from the client browser directly to the remote server, so the cookie needs to be stored in the browser's cookie store.

 

However, I've found a solution. I've been able to get the whole thing working using HttpRequest from the PECL extensions. It contains methods for easy access to cookies received from requests.

 

Thanks,

J

 

Great! Glad to hear you resolved your own issue!!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.