简体   繁体   English

PHP Cookie重定向问题

[英]PHP cookies redirect issue

I'm not exactly sure what the problem is but the cookies aren't available for subdomains. 我不确定问题出在哪里,但Cookie无法用于子域。 Is there any way around this, or is there a better way to set the cookies domain? 有没有解决的办法,还是有更好的方法来设置cookie域? Thanks! 谢谢!

<?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(); 
} 
}

Try using $domain parameter of setcookie. 尝试使用setcookie的$ domain参数。

setcookie('city', $_POST['city'], $cookie_expire, '/', '.example.com'); // With leading dot for old browsers

See here: http://php.net/setcookie 看到这里: http : //php.net/setcookie

如果Cookie路径为http://www.example.com/,并且您重定向到http://www.example.com (请注意缺少尾部/),请检查Cookie上的路径,某些浏览器会删除Cookie-在我的经验中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM