在PHP中通过子域进行共享会话 [英] sharing session over subdomains in PHP
问题描述
我使用CI。
我想通过子域共享会话
我正在使用数据库存储会话
I am using CI. I want to share session over subdomains And I'm using database to store sessions
我试过了
ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
if(session_id ==''){session_start();}
这意味着xycom和zycom将使用公共会话
帮助我,pls
That means x.y.com and z.y.com will use common session help me, pls
推荐答案
已经知道你的域名,有什么原因你使用substr来确定它吗?如果你刚刚使用,你可以更容易阅读:
Considering the fact that you already know your domain name, is there a reason why you use substr to determine it? You could would be much more readable if you just used:
ini_set('session.cookie_domain', '.domain.tld');
对于跨多个子域的Cookie,Cookie域必须以点(。)开头,后跟所有子域的公共部分(最可能是domain.tld。)
For cookies to work across multiple subdomains, the cookie domain must start with a dot (.) followed by the common part of all the sub-domains (most likely domain.tld.)
此外,您的帖子的第二行,您检查会话是否需要开始是错误的。您在session_id后缺少一组括号,因为它是一个函数,而不是常数。条件语句(if)总是会失败,导致每次都调用session_start()。
Also, the second line of your post, the one where you check if the session needs to be started is wrong. You're missing a set of parentheses after session_id because it's a function and not a constant. The conditional statement (if) would always fail causing session_start() to be called every time.
if ( empty(session_id()) ) session_start();
这篇关于在PHP中通过子域进行共享会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!