47

The example on PHP manual shows how you can use stream contexts to send a cookie. Here is the excerpt:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

How do you send more than one cookie? Like #1 or #2, or what?

#1

"Cookie: user=3345&pass=abcd\r\n"

#2

"Cookie: user=3345\r\n" . 
"Cookie: pass=abcd\r\n"
Martijn
  • 15,791
  • 4
  • 36
  • 68
Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127

2 Answers2

67

#3

Cookie: user=3345; pass=abcd
mvds
  • 45,755
  • 8
  • 102
  • 111
  • 5
    Also, keep in mind that the string before the equal sign should be the name of the cookie, anything following the equal sign should be the value. Sometimes cookies are named lets say "logininfo" and the value contains "username=123&password=123" so your header would look like "Cookie: logininfo=username=123&password=123" – RugerSR9 Jul 15 '14 at 14:28
25

Both aren't correct. You separate them with ;:

Cookie: user=3345; pass=abcd
NikiC
  • 100,734
  • 37
  • 191
  • 225