PHP / Apache 如何从 php.ini 外部设置 session.cookie_domain
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1167130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP / Apache how to set session.cookie_domain from outside php.ini
提问by stukerr
I need to restrict cookies to my www subdomain, this works by me adding the line session.cookie_domain = www.example.com in the php.ini file. However I have a number of virtual hosts on my server so I need this domain to be different for each one. After a bit of a web-seach, I have tried using:
我需要将 cookie 限制到我的 www 子域,这是通过在 php.ini 文件中添加行 session.cookie_domain = www.example.com 来实现的。但是,我的服务器上有许多虚拟主机,因此我需要每个域都不同。经过一番网络搜索,我尝试使用:
'SetEnv session.cookie_domain www.example.com' - in my httpd.conf
'php_flag session.cookie_domain www.example.com' in .htaccess
However both seem to stop cookies working all together!
然而,两者似乎都停止了 cookie 一起工作!
Any help much appreciated!
非常感谢任何帮助!
Stu
斯图
采纳答案by Andrew Moore
The easiest way to achieve this is to use session_set_cookie_params()instead of setting it via .htaccess(the .htaccessmethod only works if PHP is used as a module). You can use it in the following way:
实现这一点的最简单方法是使用session_set_cookie_params()而不是通过设置它.htaccess(该.htaccess方法仅在 PHP 作为模块使用时才有效)。您可以通过以下方式使用它:
session_set_cookie_params(0, '/', 'www.example.com');
session_start();
回答by geoffreyd
The problem is that php_flagis actually meant only for Boolean values. So when using the php_flagcommand, you're actually setting cookie_domainto 0, which is why it stops working.
问题是这php_flag实际上仅适用于布尔值。因此,在使用该php_flag命令时,您实际上是在设置cookie_domain为0,这就是它停止工作的原因。
For text values, you need to use php_valuein the .htaccess or apache config. Also quoting the value is recommended:
对于文本值,您需要php_value在 .htaccess 或 apache 配置中使用。还建议引用该值:
php_value session.cookie_domain ".domain.com"
回答by Jeff Busby
回答by Paulo Capelo
In my case this one worked for me:
在我的情况下,这个对我有用:
setcookie("name", $Value4Name, time()+00 , "/", ".domain.com");
But this is for you to record a cookie for the root of the domain.
但这是为您记录域根的cookie。
cheers PC
欢呼电脑
回答by ValeriusSoft
This code 100% work, set this code in file .htaccess
此代码 100% 工作,在文件 .htaccess 中设置此代码
php_value session.cookie_path "/"
php_value session.cookie_domain ".localhost"
Or code for file JavaScript
或文件 JavaScript 的代码
function my_cookie($name, $value, $expires = 0) {
return setcookie($name, $value, $expires, '/', 'example.com', ...);
}

