$_SESSION 跨子域
有谁知道如何跨子域保持会话?
我可以使用两个不同的子域访问特定的 cookie,但问题仍然存在...当我尝试将子域的 $_SESSION['test'] 发送到另一个子域时,它不会显示在另一个中:
subdomain1.domain .com/Trial.php
<?php
ini_set("session.cookie_domain", ".domain.com"); // allow access to this cookie from any subdomain
session_start(); // create cookie
$_SESSION['test'] = "TEST1"; // lets try to send "TEST1" in the other subdomain
?>
subdomain2.domain.com/Trial.php
<?php
ini_set("session.cookie_domain", ".domain.com");
session_start(); // cookie already set
print_r($_SESSION); // $_SESSION array is empty
?>
谢谢, 米尔凯尔
did anyone know how to keep a session across subdomains?
i can access a specific cookie using two different subdomains, but a problem remains ... when i try to send a $_SESSION['test'] for a subdomain to another , it doesn't show up in the other :
subdomain1.domain.com/trial.php
<?php
ini_set("session.cookie_domain", ".domain.com"); // allow access to this cookie from any subdomain
session_start(); // create cookie
$_SESSION['test'] = "TEST1"; // lets try to send "TEST1" in the other subdomain
?>
subdomain2.domain.com/trial.php
<?php
ini_set("session.cookie_domain", ".domain.com");
session_start(); // cookie already set
print_r($_SESSION); // $_SESSION array is empty
?>
thanks,
milkael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好将会话存储在数据库中,并将其作为 cookie 传递。 PHP 的 setcookie() 中有一个参数(域)来使 cookie 可用整个域(包括子域)。
因此,假设您的域和子域访问相同的数据库(或者至少可以访问保存会话的数据库):
Better store sessions in the database, and pass it around as cookies. PHP has a parameter (domain) in it's setcookie() to make a cookie available to the whole domain (including subdomains).
so, provided that your domain and subdomains access the same database (or at least has access to the database that holds sessions):