Cookie未在标头请求文件中设置

时间:2016-09-26 12:18:27

标签: php cookies

目前我想访问需要Cookie的网址。没有Cookie它会重定向到其他网址。我尝试在请求标头中设置Cookie,但只获取响应标头中的Cookie。不知道为什么会发生。

这是响应标题:

Cache-Control:no-store, no-cache, must-revalidate, post-check=0,   pre-check=0
    Content-Type:text/html; charset=UTF-8 Date Mon, 26 Sep 2016 11:52:59 GMT    Expires 
    Thu, 19 Nov 1981 08:52:00 GMT
    Location    
    quotev3.php?sub=compare
    Pragma  
    no-cache
    Server  
    nginx
    Set-Cookie  
    PHPSESSID=e2dnbfjvhi29e7qnkm0jbnekc4; path=/
    country=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0
    X-Firefox-Spdy  
    h2

请求标题

   Accept   
*/*
Accept-Encoding 
gzip, deflate, br
Accept-Language 
en-US,en;q=0.5
Connection  
keep-alive
Content-Length  
119
Content-Type    
application/x-www-form-urlencoded
Host    
www.parcelmonkey.co.uk
Origin  
null
User-Agent  
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0

我在标题文件中设置了Cookie。

    $cookie=$_COOKIE['PHPSESSID'];
ob_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');
header('Cookie:'.$cookie); 

任何解决方案都会非常谨慎。谢谢

1 个答案:

答案 0 :(得分:1)

您需要指定cookie的密钥,而不仅仅是它的值。

此外,该命令为Set-Cookie而非Cookie

使用setcookie功能,或header,如下所示:

setcookie('PHPSESSID', $cookie);

header("Set-Cookie: PHPSESSID={$cookie};");