Cookie没有被设置 - PHP

时间:2014-04-28 23:09:00

标签: php session cookies

**更新 - 解决了问题**我的代码是正确的,除了我需要指定目录。 cookie的文件设置位于子域中,因此跳过根目录。我更改了代码以将cookie设置为:

setcookie('Remember','remembercheck',time()+3600,'/','.mydomain.com');

现在设置正确。

**结束更新**

当我尝试登录而不检查“记住我”按钮时,cookie未被设置且我没有登录。如果我登录时选中了记住我按钮,它会正常登录。有任何想法吗?我很难过!

PHP:

<?php

require 'connect.php';
require 'functions.php';

session_name('login');

session_start();

// ---------- LOGIN ---------- 

if($_POST['submit']=='Login')
{   
    // Checking whether the Login form has been submitted

    $err = array();
    // Will hold our errors


    if(!$_POST['username'] || !$_POST['password'])
        $err[] = 'All the fields must be filled in!';

    if(!count($err))
    {
        $_POST['username'] = mysql_real_escape_string($_POST['username']);
        $_POST['password'] = mysql_real_escape_string($_POST['password']);
        $_POST['remembercheck'] = (int)$_POST['remembercheck'];

        $storedsaltquery = mysql_fetch_assoc(mysql_query("SELECT rand FROM membertable WHERE usr = '".$_POST['username']."'"));
        $storedsalt = $storedsaltquery['rand'];

        // Escaping all input data

        $row = mysql_fetch_assoc(mysql_query("SELECT id,usr, FROM membertable WHERE usr='{$_POST['username']}' AND pass='{$_POST['password']}'"));

        if($row['id'])
        {
            // If everything is OK login

            $_SESSION['usr']=$row['usr'];
            $_SESSION['id'] = $row['id'];
            $_SESSION['remembercheck'] = $_POST['remembercheck'];

            // Store some data in the session

            setcookie("Remember","remembercheck");

        }
        else $err[]='Wrong username and/or password!';
    }

    if($err)
    $_SESSION['msg']['login-err'] = implode('<br />',$err);
    // Save the error messages in the session

    echo header("Location: ../index.php");
    exit;
}

HTML:

<?php

require 'includes/connect.php';
require 'includes/functions.php';

session_name('crmLogin');
// Starting the session

session_start();

if($_SESSION['id'] && !isset($_COOKIE['Remember']) && !$_SESSION['remembercheck'])

{
    // If you are logged in, but you don't have the Remember cookie (browser restart)
    // and you have not checked the remembercheck checkbox:

    $_SESSION = array();
    session_destroy();

    // Destroy the session
}


if(isset($_GET['logoff']))
{
    $_SESSION = array();
    session_destroy();

    header("Location: index.php");
    exit;
}
?>

    <form name="login" action="includes/logincheck.php" method="post">

                Username: <input type="text" name="username" id="username" /><br>
                Password: <input type="password" name="password" id="password" /><br>
                <input name="remembercheck" id="remembercheck" type="checkbox" checked="checked" value="1" />&nbsp;&nbsp;Remember Me&nbsp;&nbsp;
                <input type="submit" value="Login" name="submit"/><br><br><br>
   </form>

1 个答案:

答案 0 :(得分:0)

使用setcookie函数我们应该设置一个到期时间

请看说明:

  

bool setcookie (string $ name [,string $ value [,int $ expire = 0 [,string $ path [,string $ domain [,bool $ secure = false [,bool $ httponly] = false]]]]]])

到期

  

Cookie过期的时间。这是一个Unix时间戳,所以数量也是如此   自纪元以来的几秒钟。换句话说,你很可能会设置   这与time()函数加上你之前的秒数   希望它过期。或者你可以使用mktime()。时间()+ 60 * 60 * 24 * 30会   将Cookie设置为在30天后过期。如果设置为0,或省略,则   cookie将在会话结束时(浏览器时)到期   关闭)。

所以你应该这样做:

setcookie("pfRemember","rememberPFcheck",time() + 3600)

更多关注php.net

相关问题