2012-08-04 57 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將在瀏覽器關閉後消失。