PHP - 使用file_get_contents发送cookie

Maj*_*our 40 php

PHP手册上的示例显示了如何使用流上下文发送cookie.以下是摘录:

// 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);
Run Code Online (Sandbox Code Playgroud)

你如何发送多个cookie?像#1或#2,或者什么?

#1

"Cookie: user=3345&pass=abcd\r\n"
Run Code Online (Sandbox Code Playgroud)

#2

"Cookie: user=3345\r\n" . 
"Cookie: pass=abcd\r\n"
Run Code Online (Sandbox Code Playgroud)

mvd*_*vds 58

#3

Cookie: user=3345; pass=abcd
Run Code Online (Sandbox Code Playgroud)

  • 另外,请记住,等号前面的字符串应该是cookie的名称,等号后面的任何内容都应该是值.有时cookie命名为"logininfo",值包含"username = 123&password = 123",因此您的标题看起来像"Cookie:logininfo = username = 123&password = 123" (4认同)

Nik*_*kiC 22

两者都不正确.你把他们分开;:

Cookie: user=3345; pass=abcd
Run Code Online (Sandbox Code Playgroud)