简体   繁体   English

跨服务器Cookie,PHP

[英]Cross Server Cookies, PHP

We have 2 web servers, one secure and one normal. 我们有2台Web服务器,一台安全,一台普通。

Is it possible to set a cookie like this 是否可以像这样设置Cookie

setcookie("basket[id]", $newID, time()+60*60*24, "/", SITE_URL, 0, true);  
setcookie("basket[id]", $newID, time()+60*60*24, "/", SECURE_SITE_URL, 1, false);

Where 哪里

SITE_URL = www.sitename.com  
SECURE_SITE_URL = xxxxx.securesitename.com

Kyle 凯尔

Set Cookie 设置Cookie

With setcookie you can set the domain parameter to indicate where the cookie is available. 使用setcookie可以设置domain参数以指示cookie在何处可用。 To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. 要使cookie在example.com的所有子域中都可用,则将其设置为“ .example.com”。 The . 的。 is not required but makes it compatible with more browsers. 不是必需的,但使其与更多浏览器兼容。

As long as your servers are referred to with different sub-domains, you can set your cookies accordingly. 只要您的服务器使用不同的子域引用,就可以相应地设置cookie。

Secure parameter 安全参数

Secure, indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. 安全,表示该cookie仅应通过来自客户端的安全HTTPS连接进行传输。

Setting a Cookie on a different domain 在其他域上设置Cookie

A server cannot set a cookie for a domain that it isn't a member of. 服务器无法为其不属于其的域设置cookie。

The server issuing the cookie must be a member of the domain that it tries to set in the cookie. 发出cookie的服务器必须是它​​试图在cookie中设置的域的成员。 That is, a server called www.myserver.com cannot set a cookie for the domain www.yourserver.com. 也就是说,名为www.myserver.com的服务器无法为域www.yourserver.com设置cookie。

How Double Click do it 双击的方式

One exception to the rule is ad agency Double click. 该规则的一个例外是广告代理商双击。 Who manage to add cookies to your PC without you visiting the specific web site by packaging cookies with image requests when they are loaded from their servers onto other peoples web sites. 谁设法在您不访问特定网站的情况下将cookie添加到您的PC,方法是将cookie与图像请求一起打包(当它们从其服务器加载到其他人的网站时)。

You cannot set a cookie for a domain other than the current or a superset of it (like example.com is a superset of foo.example.com and bar.example.com ). 您不能为当前域或它的超集(例如example.comfoo.example.combar.example.com的超集)以外的域设置Cookie。 That means the second Set-Cookie will get rejected by the browser. 这意味着第二个Set-Cookie将被浏览器拒绝。

One solution is to use a subdomain of your main domain for your secure domain, like secure.example.com . 一种解决方案是将主域的子域用于安全域,例如secure.example.com Then a cookie set for .example.com would be available at www.example.com as well as at secure.example.com . 然后,可以在www.example.com以及secure.example.com上获得为.example.com设置的cookie。

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

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