1

我想在登录时存储 cookie 以记住我的概念。它工作正常,但是当我注销 cookie 时也会破坏。我的代码如下...

登录页面:

 <?php
if(isset($_POST['submitLogin']))
    {
        extract($_POST);
        $obj = new validation();
        $obj->add_fields($uname, 'req',ER_EMAIL );
        $obj->add_fields($passwd, 'req', ER_PSW);
        $error = $obj->validate();
        $password=md5($passwd);

           $qry1=$con->recordselect("SELECT *,concat(first_name,' ',last_name) as user_name FROM user WHERE uname='".addContent($uname)."' AND password='".addContent($password)."'");
             $tot_rec=mysql_num_rows($qry1);
             $valid_user=mysql_fetch_array($qry1);

             if($tot_rec>0)
             {                      
                if($valid_user['status']=='a')
                   {                         
                     if($valid_user['password_status'] == 0)
                     {
                          $_SESSION["userId"]=$valid_user['id'];
                          $_SESSION["user_name"]=$valid_user['user_name'];

                           if($error=='')
                            {
                                 if (isset($_POST['rememberme'])) {

                                setcookie('uname',$_POST['uname'],time() + (24 * 60 * 60 * 30));
                                setcookie('passwd',$_POST['passwd'],time() + (24 * 60 * 60 * 30));
                                setcookie('rememberme',1,time() + (24 * 60 * 60 *30 ));

                                }
                                else
                                {
                                    setcookie('uname',$_POST['uname'],time() - (24 * 60 * 60 * 30));
                                    setcookie('passwd',$_POST['passwd'],time() - (24 * 60 * 60 * 30));
                                    setcookie('rememberme',1,time() - (24 * 60 * 60 * 30));
                                }

                            }
                          redirectPage(home.php");

                     }


                   }
                else
                {
                        $error=ER_DACT;
                        redirectPage("login.php?Err=ER_DACT");

                }
             }
             else{
             $error=ER_INVUP;
             redirectPage("login.php?Err=ER_INVUP");
             }

         }

?>

这是我的注销页面:

<?php

    $_SESSION["userId"]="";
    $_SESSION["name"]="";
    session_destroy();
    redirectPage(login.php);

?>

更新

此处的 HTML 代码:

<form action="login.php" method="post" name="frmCP1" id="frmCP1">
                <div class="space10"></div><!--space10-->


                <div class="text18blue">Student Login</div>
                <div class="h-line"></div>

                <div><strong>Username:</strong> <span class="errortext">*</span></div>
                <div>
                    <input name="uname" id="uname" type="text"   value="<?php if(isset($_COOKIE['uname'])&&$_COOKIE['uname']!=''){echo $_COOKIE['uname'];}else {echo '';} ?>"  />   
                </div>
                <div class="space10"></div>
                <div><strong>Password:</strong> <span class="errortext">*</span></div>
                <div><input  id="passwd"  name="passwd" type="password" value="<?php if(isset($_COOKIE['passwd'])&&$_COOKIE['passwd']!=""){echo $_COOKIE['passwd'];}else {echo "";} ?>" /></div>
                <div class="space10"></div>
                <div class="checkbox fl"><input <?php if(isset($_COOKIE['rememberme']) && $_COOKIE['rememberme']=="1"){echo "checked='checked'";} ?>  id="remember" name="rememberme"  type="checkbox" value="1"/></div>
                <div>Remember me</div>
                <div class="space10"></div>
                <div><input id="submitLogin" name="submitLogin" type="submit" value="Login" /></div>
                <div class="space10"></div>
                <div><a href="javascript:void(0)" id="dialog_link">Forgot password?</a></div>
                <div class="flclear"></div>

        </form>

所以当我注销我的cookie时也会破坏为什么?提前致谢

4

1 回答 1

2

情况不应该如此。销毁会话只应该销毁会话而不是 cookie

尝试添加路径信息以获得更好的性能

setcookie('uname',$_POST['uname'],time() - (24 * 60 * 60 * 30), '/');
setcookie('passwd',$_POST['passwd'],time() - (24 * 60 * 60 * 30), '/');
setcookie('rememberme',1,time() - (24 * 60 * 60 * 30), '/');

这肯定会解决你的问题

于 2013-04-13T06:33:01.833 回答