【问题标题】:PHP cookies redirect issuePHP cookie 重定向问题
【发布时间】: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(); 
} 
}

【问题讨论】:

    标签: php redirect cookies


    【解决方案1】:

    尝试使用 setcookie 的 $domain 参数。

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

    请看这里: http://php.net/setcookie

    【讨论】:

    • 好的,在你的例子中错过了第一行。你能提供更多信息吗?域,还是演示?你用什么浏览器检查?
    • 第一行是什么意思?抱歉,我的网站不对外公开。我用 IE、Firefox 和 Chrome 测试过。
    • 我的意思是,我错过了“ini_set”行。您的代码没有问题,因此您的环境中可能存在其他问题。
    • 我应该把它放在哪里?能给我完整的剧本吗?谢谢!
    • 我已经使用一些测试脚本测试了服务器并且它正在工作。
    【解决方案2】:

    如果cookie路径是http://www.example.com/,检查cookie上的路径 然后你重定向到http://www.example.com(注意缺少尾随 / )一些浏览器会丢弃 cookie - 根据我的经验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多