简体   繁体   English

PHP Cloudfront SetCookie

[英]AWS Cloudfront SetCookie in PHP

I am trying to set a cookie to view private content from AWS Cloudfront 我正在尝试设置cookie以查看来自AWS Cloudfront的私有内容

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-setting-signed-cookie-custom-policy.html http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-setting-signed-cookie-custom-policy.html

They give an example cookie header: 他们给出了一个示例cookie标题:

Set-Cookie: Domain=d111111abcdef8.cloudfront.net; Path=/; Secure; HttpOnly; CloudFront-Key-Pair-Id=APKA9ONS7QCOWEXAMPLE

I created the following php code 我创建了以下PHP代码

setcookie (
  'CloudFront-Key-Pair Id',
  'MYID',
  0,
  '/',
  'mycloudfrontsub.cloudfront.net',
  true, 
  true
);

But the cookie is not set. 但是没有设置cookie。 The cookie is only set if I take out the domain name. 只有在我取出域名时才设置cookie。

I think this is due to calling the setcookie in a script after session_start. 我认为这是因为在session_start之后在脚本中调用了setcookie。 I tried adding this, but it's required before session_start() 我尝试添加这个,但是在session_start()之前需要它

session_set_cookie_params(0, '/', 'duvoxso6rm38g.cloudfront.net);

Do I need to do something like this? 我需要做这样的事情吗?

//close local session, then open new one for aws
$id=SID;
session_write_close();
session_set_cookie_params(0, '/', 'mysub.cloudfront.net');
session_start();
setcookie(...);
session_write_close();
session_set_cookie_params(0, '/', 'originaldomain.com');    
session_start();

The cookies do not show up in the browser because you send a cookie for a domain A from domain B. That is silently ignored by the browsers for security reasons. Cookie不会显示在浏览器中,因为您从域B发送域A的cookie。出于安全原因,浏览器会默默忽略这些cookie。 This is a browser feature not a PHP thing. 这是一个浏览器功能而不是PHP的东西。

If you want to use signed cookies with CloudFront you need to use a CNAME for the CloudFront distribution that is a subdomain of you PHP server domain. 如果您要在CloudFront中使用已签名的Cookie,则需要为CloudFront分配使用CNAME,该分发是您的PHP服务器域的子域。 A more detailed answer can be found under: https://mnm.at/markus/2015/04/05/serving-private-content-through-cloudfront-using-signed-cookies/ 更详细的答案可以在以下网址找到: https//mnm.at/markus/2015/04/05/serving-private-content-through-cloudfront-using-signed-cookies/

In short: assume you want to use the domain example.com . 简而言之:假设您要使用域example.com You serve the PHP files under www.example.com . 您在www.example.com下提供PHP文件。 Then you can use the CNAME media.example.com for the d111111abcdef8.cloudfront.net. 然后,您可以将CNAME media.example.com用于d111111abcdef8.cloudfront.net。 To send the cookies from the PHP side to the CloudFront Server you need to use the domain example.com in the Cookie. 要将Cookie从PHP端发送到CloudFront服务器,您需要在Cookie中使用域example.com

The referenced site does also notice that you should use the PHP function header() to send the cookie, not setcookie() . 引用的站点也注意到你应该使用PHP函数header()来发送cookie,而不是setcookie() This is because the setcookie function does some encoding that ruins the singing parameter created with the functions mentioned on the site. 这是因为setcookie函数执行了一些编码,这些编码会破坏使用网站上提到的功能创建的歌唱参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM