Jump to content

Recommended Posts

I've been using stream_context_create() to send cookies along with a file_get_contents() call when requesting a remote page.  This is useful if I need to pass information to the remote page that my server can't pass, like a login form.  This works perfectly well when I've got a single cookie, or multiple cookies on the same domain and path, but what if I need to send multiple cookies that work for multiple different domains or paths?

 

Here's an example code:

 

<?php
$cookies = array("Cookie: testcookie=blah; testcookie2=haha; path=/; domain=infectionist.com;");
$opts = array('http' => array('header' => $cookies));
$context = stream_context_create($opts);
$html = file_get_contents("http://infectionist.com/misc/testing/cookie.php?do=view", false, $context);
echo $html;
?>

 

This will utilize a quick script that I wrote that displays the values of 2 cookies named "testcookie" and "testcookie2".  Notice in the cookies array, I've got both cookies set, and since both work on the same path and domain, they both appear properly.  But I've got a page that I need to retrieve while sending multiple cookies from multiple domains (actually subdomains of the requested domain) and/or paths.

 

How might I accomplish this?  I tried making my cookies array have multiple values, like this:

 

$cookies = array("Cookie: testcookie=blah; path=/; domain=infectionist.com;", "Cookie: testcookie2=haha; path=/; domain=infectionist.com;");

 

But that didn't work, only the first cookie was set.  I can't seem to figure out how this might be done, but I'm sure someone else has done it before.  Any help with this will be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/198900-sending-cookies-with-file_get_contents/
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.