处理两个PHP会话cookie? [英] Dealing with two PHP session cookies?
问题描述
我正在尝试处理两个PHPSESSID cocokies.一个使用www子目录(即www.mydomain.com),而另一个使用.mydomain.com.
I'm trying to deal with two PHPSESSID cocokies. One uses the www subdirectory - so www.mydomain.com - while the other uses .mydomain.com.
就目前情况而言,该脚本能够设置cookie域,但是如果在访问mydomain.com之前在www子域中运行了另一个脚本,则将cookie设置为www.mydomain.com.然后,如果我访问mydomain.com,则会设置.mydomain.com的Cookie.这意味着我可以得到两个PHPSESSID cookie.
As it stands now the script is able to set the cookie domain, but if another script is ran at the www subdomain before I access mydomain.com, then the cookie is set for www.mydomain.com. Then if I visit mydomain.com a cookie for .mydomain.com is set. This means that I can end up with two PHPSESSID cookies.
有没有办法确定在这种情况下我正在处理哪个cookie?
Is there a way to be sure of which cookie I'm dealing with in a scenario like this?
我看了另一篇文章,但没有得出任何结论.
I've looked at another post but didn't come away with anything conclusive.
How to handle multiple cookies with the same name?
推荐答案
在会话开始之前,将其放在运行的第一个php文件(例如index.php或config.php文件)的顶部.
Put this at the top of the first php file that runs, like index.php or a config.php file.. before the session starts.
<?php
if(stripos($_SERVER['HTTP_HOST'],'www')===false) {
ini_set('session.cookie_domain', 'site.com');
} else {
ini_set('session.cookie_domain', 'www.site.com');
}
?>
这将导致该cookie仅与1个或其他域相关联,这意味着用户可以拥有2个名为PHPSESSID的cookie.
This will cause the cookie to only be associated with 1 or the other domains, meaning that the user can have 2 cookies named PHPSESSID.
这篇关于处理两个PHP会话cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!