且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在同一服务器上的多个域中维护PHP会话?

更新时间:2023-11-22 23:46:28

根据修改PHP变量的首选方法(Apache的配置.htaccess),将session.cookie_domain值更改为一致的值.

Depending upon your preferred method of modifying PHP variables (Apache's config, .htaccess), change the session.cookie_domain value to be a consistent value.

我有多个子域,Apache配置文件中的每个VirtualHost部分都包含以下行:

I have multiple sub-domains, and each VirtualHost section in the Apache config file contains the following line:

php_value session.cookie_domain mydomain.com

php_value session.cookie_domain mydomain.com

如果在.htaccess文件中进行更改,语法应相似.

The syntax should be similar if you make the changes in a .htaccess file.

已更新bobert5064的评论:

Updated for bobert5064's comment:

对于多个域(即domain1.com,domain2.org),我认为只需要选择一个通用域名(即domain1.com).我从未尝试过这样做,因此无法验证它是否有效,但是逻辑似乎正确.

For multiple domains (ie domain1.com, domain2.org), I think it is only necessary to choose a common domain name (ie domain1.com). I have never tried this, so I cannot verify that it works, but the logic seems accurate.

http://us.php.net/manual/en/function.session-set-cookie-params.php .该文档未提及在其他域上设置cookie的能力.

There is also a method to set the variables direction in PHP described at http://us.php.net/manual/en/function.session-set-cookie-params.php. The documentation makes no reference to the ability or inability to set cookies on a different domain.