One of Yahoo's performance rules says: Use cookie-free domains for static components. This is good because the server has no use for cookie information when serving a JPEG or another static component, so all this cookie information creates network traffic for no reason.
Yahoo的一项性能规则说:对静态组件使用无cookie域。 这很好,因为在为JPEG或其他静态组件提供服务时,服务器不使用Cookie信息,因此所有这些Cookie信息无缘无故会产生网络流量。
One of the implications of following the rule is related to the whole www vs no-www question. Basically you should always use www if you're planning to use any other sub-domains and you want them cookie-free. This is because you have no way to set a cookie only to the top-level domain. So for example you cannot write a cookie only to phpied.com. If you load component from img.phpied.com, the cookie from phpied.com will be sent again. In Firefox this doesn't seem to be the case, but in IE it is.
遵循规则的含义之一与整个www与no-www问题有关。 基本上,如果您打算使用任何其他子域并且希望它们不包含cookie,则应始终使用www。 这是因为您无法将Cookie仅设置到顶级域。 因此,例如,您不能仅将cookie写入phpied.com。 如果从img.phpied.com加载组件,则将再次发送phpied.com的cookie。 在Firefox中似乎并非如此,但在IE中却是如此。
Here are two test scripts that demonstrate the behavior:
这是两个演示行为的测试脚本:
Load both of these pages and then reload them to see what cookies are sent. They both try to set cookies in all possible ways:
加载这两个页面,然后重新加载它们以查看发送了哪些Cookie。 他们都尝试以所有可能的方式设置cookie:
- omitting the domain name 省略域名
- .domain.com.domain.com
- domain.com domain.com
- www.domain.com www.domain.com
Here's the source code of the files:
这是文件的源代码:
nowww.php [test]
nowww.php [测试]
<?php
setcookie('no0', 'no www, no domain');
setcookie('no1', 'no www, .phpied.com', 0, '/', '.phpied.com');
setcookie('no2', 'no www, phpied.com', 0, '/', 'phpied.com');
setcookie('no3', 'no www, www.phpied.com', 0, '/', 'www.phpied.com');
echo '<pre>';
print_r($_COOKIE);
?>
yeswww.php [test]
yeswww.php [测试]
<?php
setcookie('yes0', 'yes www, no domain');
setcookie('yes1', 'yes www, .phpied.com', 0, '/', '.phpied.com');
setcookie('yes2', 'yes www, phpied.com', 0, '/', 'phpied.com');
setcookie('yes3', 'yes www, www.phpied.com', 0, '/', 'www.phpied.com');
echo '<pre>';
print_r($_COOKIE);
?>
Loading the two pages twice shows how in IE, no0, no1, and no2 are all visible when using www as well as when not using it. In Firefox it's almost the same, only that no0 is not visible when using www.
两次加载两个页面显示了在IE中使用www以及不使用www时,no0,no1和no2都可见。 在Firefox中几乎是一样的,只是使用www时no0不可见。
As a take-home:
实话实说:
- use www 使用www
- write cookies to the appropriate domain level (e.g. don't write to *.domain.com) 将Cookie写入适当的域级别(例如,不要写入* .domain.com)
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息