jasonc2 Posted February 13, 2009 Share Posted February 13, 2009 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 https://forums.phpfreaks.com/topic/145096-php-528-retrieve-cookies-from-remote-host-and-pass-them-back-to-client/ Share on other sites More sharing options...
allworknoplay Posted February 13, 2009 Share Posted February 13, 2009 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? Link to comment https://forums.phpfreaks.com/topic/145096-php-528-retrieve-cookies-from-remote-host-and-pass-them-back-to-client/#findComment-761445 Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 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 https://forums.phpfreaks.com/topic/145096-php-528-retrieve-cookies-from-remote-host-and-pass-them-back-to-client/#findComment-761490 Share on other sites More sharing options...
jasonc2 Posted February 13, 2009 Author Share Posted February 13, 2009 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 https://forums.phpfreaks.com/topic/145096-php-528-retrieve-cookies-from-remote-host-and-pass-them-back-to-client/#findComment-761544 Share on other sites More sharing options...
allworknoplay Posted February 13, 2009 Share Posted February 13, 2009 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 https://forums.phpfreaks.com/topic/145096-php-528-retrieve-cookies-from-remote-host-and-pass-them-back-to-client/#findComment-761548 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.