【问题标题】:Cookie works in Chrome but not in IE or Safari?Cookie 在 Chrome 中有效,但在 IE 或 Safari 中无效?
【发布时间】:2014-01-11 05:11:20
【问题描述】:

我在我的登录系统中使用 cookie,并且该系统在 Chrome 上运行,但是当我进入 IE 或 Safari 时,它无法运行。出于某种原因,cookie 没有被设置,我试图回应它们无济于事。

以下是制作 cookie 的代码:

if(isset($_POST['log_in_iniator'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $log_in_checker_status = check_user_data($username, $password);
    if($log_in_checker_status == 'true'){
        //user has successfully logged in, create two cookies
        //cookie 1 username
        setcookie('username', $username, 0, 'http://shkeek.com');
        setcookie('loginstatus', 'true', 0, 'http://shkeek.com');
        header("Location: index.php");
    }else{
        setcookie('loginstatus', 'invalid', 0);
        header("Location: index.php");
    }
}

【问题讨论】:

    标签: php mysql sql cookies browser


    【解决方案1】:

    变化:

     setcookie('username', $username, 0, 'http://shkeek.com');
    

    收件人:

     setcookie('username', $username, (24*60*60), '/');
    

    或者:(见下文)

     setcookie('username', $username, (24*60*60), '/', '.shkeek.com');
    

    为什么:

    您将“过期”时间设置为从现在开始的 0 秒。因此,Chrome 正在制作一个“会话”cookie(当您关闭浏览器时将过期),IE 和 FF 将完全按照您告诉他们的操作 - 使任何匹配的 cookie 过期。

    (24*60*60) 是“一天”; 24 小时 * 60 分钟 * 60 秒后。

    对于“域路径”部分,只需要设置当前服务器和服务器路径的“/”即可。

    如果站点支持多个子域,请改用'/', '.shkeek.com'。然后您就可以支持www.shkeek.comshkeek.comimg.shkeek.com.shkeek.com 的任何其他子域。

    更多详情,请查看PHP docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多