【发布时间】:2013-12-08 19:42:22
【问题描述】:
我不确定问题出在哪里,但 cookie 不适用于子域。有什么办法可以解决这个问题,还是有更好的方法来设置 cookie 域?谢谢!
<?php
ini_set("session.cookie_domain", ".example.com");
// Set cookie and redirect when user change city
if( isset($_POST['city']) && $_POST['city'] != '' ){
$cookie_expire = time() + 50400;
setcookie('city', $_POST['city'], $cookie_expire, '/');
header("Location: http://".$_POST["city"].".example.com");
die();
}
// Redirect if user selected default city
if (isset($_COOKIE["city"])) {
$subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
if ($_COOKIE["city"] != $subdomain) {
header("Location: http://".$_COOKIE["city"].".example.com");
die();
}
}
【问题讨论】: