Cookie破坏了会话毁灭,为什么?

时间:2013-04-13 05:39:02

标签: php session cookies

我想在登录时存储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时也会破坏原因? 提前谢谢

1 个答案:

答案 0 :(得分: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), '/');

这肯定会消除你的问题