在使用cookie时遇到问题,请记住我可以工作,但cookie在取消选中时不会过期


Having trouble using cookies, remember me works but cookies do not expire when unticked

以前从未实现过cookie,我遇到了一些麻烦。如果用户选中"记住我"框并登录,关闭浏览器,然后重新打开它,他们就会保持登录状态。但是,如果用户取消选中"记住我"框,登录,关闭浏览器,然后重新打开它,它仍然是登录的。我已经尝试了我能想到的一切!

<?php
  define('INCLUDE_CHECK',true);
  require 'connection.php';
  session_name('ppLogin');
  session_set_cookie_params(2*7*24*60*60);
  session_start();
  if($_SESSION['id'] && !isset($_COOKIE['ppRemember']) && !$_SESSION['rememberMe'])
  {
  $_SESSION = array();
  session_destroy();
  }
  if(isset($_GET['logoff']))
  {
  $_SESSION = array();
  session_destroy();
  header("Location: index.php");
  exit;
  }
  if($_POST['submit']=='Login')
  {
  $err = array();
  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['rememberMe'] = (int)$_POST['rememberMe'];
    $row = mysql_fetch_assoc(mysql_query("SELECT id,username FROM client WHERE username='".$_POST['username']."' AND password='".$_POST['password']."'"));
    if($row['username'])
    {
        $_SESSION['username']=$row['username'];
        $_SESSION['id'] = $row['id'];
        $_SESSION['rememberMe'] = $_POST['rememberMe'];
        setcookie('ppRemember',$_POST['rememberMe']);
    } else {
        $err[]='Wrong username and/or password!';
    }
}
if($err){
    $_SESSION['msg']['login-err'] = implode('<br />',$err);
} else {
    echo "sent";
}
 }
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link type="text/css" href="css/custom-theme/jquery-ui-1.8.16.custom.css" rel="Stylesheet" />
    <link type="text/css" href="css/style.css" rel="Stylesheet" />
    <link type="text/css" href="css/login.css" rel="Stylesheet" />
    <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="js/validate-login.js"></script>
</head>
<body> 
    <div id="wrap">
        <div id="header">
        </div>
        <div id="menus">
            <div id="account">
                <a href="register.php" class="ui-corner-all"> REGISTER </a>
                <a href="login.php" class="ui-corner-all"> LOG IN </a>
            </div>
            <div id="navigation-menu">
                <a href="register.php" class="ui-corner-all"> CONTACT US </a>
            </div>
        </div>
        <?php
        if(!$_SESSION['id']):
        ?>
 <div id="login">
 <form id="login-form" action="do-login.php" style="autocomplete:off" method="post">
 <div id="form-part-1" class="l-form">
 <h1>Account details</h1>
 <p>Please enter your username and password in the boxes provided below</p>
 <label for="username">Username
 <span class="small"><label id="error_1"></label></span>
 </label>
 <input type="text" name="username" id="username" maxlength='15'/>
 <label for="password">Password
 <span class="small"><label id="error_2"></label></span>
 </label>
 <input type="password" name="password" id="password" maxlength='15' />
 <label for ="rememberMe">&nbsp;Remember me</label>
 <input name="rememberMe" id="rememberMe" type="checkbox" checked="checked" value="1" /> 
 <div class="button" style="float:right;">
 <input value="Login" type="submit" name="submit" id="button_1" />
 </div>
 <div class="spacer"></div>
 </div>
 </form>
 </div>
        <?php
    else:
    ?>
<div id="already-logged-in">
<h1>You are already logged into your account</h1>
<h2>To go to your account: <a href="my-account.php" class="ui-corner-all"> My Account </a></h2>
<h3>Or log out: <a href="destroy.php" class="ui-corner-all"> Log out of My Account </a></h3>
</div>
        <?php
    endif;
    ?>
        <div id = "footer">
        </div>
    </div>
</body>
 </html>

谢谢!

当用户第一次登录时,会创建并保存一个带有到期日期的cookie。除非你写一些代码来删除它,否则这不会在到期日前删除或取消设置。如果用户取消选中复选框,我可能猜你没有删除cookie,导致脚本检索现有cookie。如果你想取消设置cookie,那么在取消选中时,你必须将cookie值设置为负数,这样现有的cookie就会被销毁。