无法识别cookie

时间:2011-12-26 04:55:07

标签: php cookies

代码如下。我最新版本的Firefox遇到了这个问题。我检查了其他论坛,但没有找到任何解决方案。我想我可能做错了什么?你可能会说,我对php有点新鲜。

此外,我知道我的方法对于此代码中的许多其他内容有点不正统。但是,我想要解决的问题是if语句测试,它检查是否设置了$ _COOKIE ['auth']。我检查了我的饼干,它是设置的。我测试了一些die()语句,并没有被提取。有什么帮助吗?

<?php


//THIS IS WHERE THE PROBLEM EVIDENTLY OCCURS
if (isset($_COOKIE['auth'])){
if ($_SESSION['auth'] > 0){
    header ("Location: /members/index.php");
} else {
    setcookie ('auth', '', time() - 3600, "/", "www.domain-here.com");
}
} else {
$old_url = $_SERVER['HTTP_REFERER'];
$username = "";
$password = "";

if ($_POST['username'] != "" && $_POST['password'] != ""){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $expire = 0;
    if ($_POST['save'] == "checked"){
        $expire = time()+60*60*24*14;
    }

    //$result = db query to check if user exists

    if ($result) {
        if (mysql_num_rows($result) > 0){
            session_start();
            setcookie("auth", $sid, $expire, "/", "www.domain-here.com"); //here I am setting the cookie to allow users to stay logged on
            header ("Location: $old_url");
        } else {
            session_start();
            $_SESSION['auth'] = "0";
            die(alert("Invalid username or password.", "login.php"));
        }
    } else {
        die(alert("Invalid username or password.", "login.php"));
    }
}
}
?>

1 个答案:

答案 0 :(得分:0)

首先关闭,你正在使用time() - 3600这意味着你的cookie小于0?接下来。你需要给它一个值,否则它不会设置cookie。这是更正后的代码。

if (isset($_COOKIE['auth'])){
if ($_SESSION['auth'] > 0){
    header ("Location: /admin/itemdev.php");
} else {
    setcookie ('auth', 'my value', time() + 3600, "/", "localhost");

}
}