2012-08-04 196 views
0

我使用PHP Session变量有一个用户登录并编辑他们的网站。当他们注销时,我呼叫session_destroy();函数来终止会话。会话和cookie,如果我销毁会话,cookie会消失吗? (PHP)

在登录屏幕,我一定要“记住此计算机”和而不是使用$_SESSION[]变量我设置这样一个cookie的选项:

setcookie("admin", true, $expire);

的Cookie设置,但是当我注销后,Cookie将取消设置。任何方式来防止这种情况?我希望会议结束,但我希望网站也记住计算机。

谢谢你的时间。

编辑:这里有两种方法我用它来启动和销毁会话(会话方法我称之为是另一个类)

public function loginCheck() { 

    //check if the remember me is set 

    if (isset($_POST['remember'])) { 
     $expire = time() + 60 * 60 * 24 * 30; 
     setcookie("admin", true, $expire); 
    } 
    else{ 
     setcookie("admin", "", time()-3600); 
    } 


    $sth = $this->db->prepare("SELECT uid FROM philllwareusers WHERE 
      usr = :login AND pasw = :password"); 

    $sth->execute(array(
     ':login' => $_POST['user'], 
     ':password' => $_POST['pswrd'] 
    )); 

    $data = $sth->fetch(); 

    $count = $sth->rowCount(); 

    if ($count > 0) { 

     //check if user has permision to edit this website 

     $sth = $this->db->prepare("SELECT web_id FROM website_spine WHERE 
      admin_id = :uid "); 
     $sth->execute(array(
      ':uid' => $data['uid'] 
     )); 

     $datas = $sth->fetch(); 

     $counts = $sth->rowCount(); 
     if ($counts > 0) { 

      if ($datas['web_id'] == WEB_ID) { 



       Session::init(); 
       Session::set('uid', $data['uid']); 
       Session::set('loggedIn', true); 
       header('location: ../index'); 
      } else { 
       header('location: ../Adminlogisn'); 
      } 
     } 

     // login 
    } else { 
     header('location: ../Adminlogin'); 
    } 
} 

function logout() { 
    Session::init(); 
    Session::destroy(); 
    header('location: '.URL.'/Adminlogin'); 
} 

这是管理员登录的样子(的一部分会检查如果cookie应设置,并应留一套或摧毁它。),

<?php 
     if(!isset($_COOKIE['admin']) || $_COOKIE['admin'] == ''){?> 
    Remember this computer as an admin computer <div style="display: inline;cursor: pointer;" onclick="alert('By selecting this option, your website will remember this computer and present the login option on the menu bar when you are not logged in.')">[?]</div> 
    <input type="checkbox" name="remember" style="width: 20px;"/> 
    <?php 

    } 
    else{ 
     ?> 
      Un-check to forget this computer as an admin computer. <div style="display: inline;cursor: pointer;" onclick="alert('This computer is currently rememberd as and admin computer, making the login link on the menu bar visible for easy access. Uncheck this box to forget this computer as admin computer.')">[?]</div> 
    <input type="checkbox" name="remember" checked="checked" style="width: 20px;"/> 
    <?php 
    } 
    ?> 
+0

如果单独设置cookie,它不会消失。看起来你在会话中检查了你的cookie的价值已经被破坏了。 – Leri 2012-08-04 16:37:41

+0

如果在用户访问该页面时未提交“记住”,则取消设置Cookie。也许这会导致cookies丢失。 – Leri 2012-08-04 16:41:18

+0

如果cookie已经存在,则记录会自动设置,用户可以通过取消选中来删除cookie,因此它会丢失页面。对于之前没有提及的问题,我非常抱歉,请查看我修改后的问题。 – 2012-08-04 16:45:07

回答

-1

这里是您可以在其中找到你的问题的答案的链接:

http://www.codeflask.com/2012/08/why-session-destroy-when-remove-my.html

和实际的答案(从链接引用)是:

会话自动销毁当cookies被删除时,原因是每当创建会话时,它也会将其ID存储在cookie中,这就是会话自动销毁的原因。

+0

此链接转到*单个句子*,您可以在答案中键入此链接,或者如果它不是您的答案,则进行总结。 – 2012-08-09 19:50:10

0

不检查你的代码。

您将不得不检查用户是否有cookie(不是会话!)。如果他使用cookie进入网站,但没有进行会话,则可以在后台登录(并设置会话)。

如果您有适当的过期时间,cookie不应该取消设置。然而,会话cookie将在浏览器关闭后消失。